A fragile coalition of lawmakers in South Africa elected Cyril Ramaphosa for a second term as president on Friday, marking a new era of political uncertainty in one of the continent’s most stable democracies.

After suffering a sharp decline in support in last month’s national election, Mr. Ramaphosa’s party, the African National Congress, undertook feverish negotiations to form a governing coalition with rivals, inking a deal only after Friday’s parliamentary session had begun.

The coalition deal includes the second-largest party, the Democratic Alliance — long a bitter rival of the A.N.C. — and the fifth-largest one, the Inkatha Freedom Party. Some of the 15 other parties in Parliament are also expected to join the coalition, which the A.N.C. is calling a “government of national unity.”

Many South Africans hope that this new coalition will force the parties to work together to provide better outcomes in a country with economic stagnation, high unemployment and entrenched poverty.

Addressing lawmakers after a 14-hour session, Mr. Ramaphosa said the fact that opposing parties decided to come together to elect him “has given a new birth, a new era to our country.”

“I do sincerely believe that this is an era of hope and is also an era of inclusivity,” he added.

The Democratic Alliance — which won nearly 22 percent of the vote — hailed the coalition agreement as a fresh start. “From today, the D.A. will co-govern the Republic of South Africa in a spirit of unity and collaboration,” said John Steenhuisen, the party’s leader.

But the new government faces many challenges.

The A.N.C. had governed South Africa with comfortable majorities since the end of apartheid in 1994. In that first government, led by Nelson Mandela, the A.N.C. included rival parties as a show of unity. In this year’s election, the party captured only 40 percent of the vote, so if the parties clash, the government could collapse.

Such an outcome would echo a pattern seen in municipalities across South Africa, where local coalitions sometimes last only months — or even weeks — leaving voters frustrated and disillusioned.

Some of the parties in the new government are already sharply divided on ideology and policy. The A.N.C. struggled to sell a partnership with the Democratic Alliance to many of its members, who fear the D.A. and its mostly white leadership will attempt to roll back affirmative action efforts in South Africa.

By the time the 400 members of Parliament convened on Friday morning at a convention center along the Atlantic coast in Cape Town, it was still unclear whether Mr. Ramaphosa had secured a second term.

UMkhonto weSizwe, a party led by Jacob Zuma, the former president of South Africa and the A.N.C., boycotted the session — its 58 seats sitting empty.

Mr. Zuma had a bitter falling out with Mr. Ramaphosa, his former deputy, after he was forced to resign as president in 2018 amid corruption allegations. Mr. Zuma has claimed, without providing evidence, that the recent national election was rigged and that his party, the third-largest in Parliament, won far more than the nearly 15 percent tallied by the electoral commission.

Mr. Zuma has said that Mr. Ramaphosa must resign if the A.N.C. wants his party, known as M.K., to join the coalition, a demand that A.N.C. officials have rejected.

The Economic Freedom Fighters, another party with roots as a breakaway group from the A.N.C., also spurned the call for unity. The party’s leader, Julius Malema, was expelled from the A.N.C. in 2012. He has said he would refuse to join a coalition that included the Democratic Alliance.

“We reject this government,” Mr. Malema said of the new coalition, arguing that the Democratic Alliance supports racist policies.

Instead of joining the A.N.C.’s unity effort, Mr. Malema’s party has teamed up with five other parties, calling themselves a new progressive caucus.

Pushback against the Democratic Alliance from within the A.N.C. forced its leaders to walk a delicate line as they sought to avoid alienating their party’s base of Black voters while also arguing that a partnership would be a sensible move for the country.

Though it vowed last year never to work with the A.N.C., the Democratic Alliance was one of the parties most eager to participate in a unity coalition. Its leaders had said it was important to prevent what they called a “doomsday coalition” between the A.N.C. and the Economic Freedom Fighters.

The Democratic Alliance embraces free-market capitalism, an approach that some A.N.C. leaders believe would attract more investors to South Africa, the continent’s largest economy. That is in contrast to some of the more aggressive wealth redistribution policies promoted by M.K. and the Economic Freedom Fighters, like nationalizing banks and seizing land from white owners without providing compensation.

In exchange for supporting Mr. Ramaphosa’s election as president, the Democratic Alliance will now hold positions in the cabinet and the National Assembly, Mr. Steenhuisen said. The A.N.C. supported the election of a deputy speaker of Parliament from the Democratic Alliance.

“The people have made it clear that they do not want any one party to dominate our society,” Mr. Steenhuisen said.

The coalition agreement also sets out guidelines for resolving disputes among the parties. It includes some elements that appear to address concerns over the Democratic Alliance’s stance on racial justice issues. The priorities set out in the deal include “inclusive and sustainable economic growth,” “creating a more just society” and “undertaking common programs against racism, sexism, tribalism and other forms of intolerance.”

Some lawmakers criticized the coalition, accusing the A.N.C. of using the term “government of national unity” as cover to form a pact with the Democratic Alliance.

Mr. Ramaphosa rejected that claim, saying the government “will be constituted not by two, not by three, but by more parties that voluntarily want to participate.”

To soften the blowback during negotiations, A.N.C. leaders also sold a partnership with the Democratic Alliance in tandem with Inkatha, a party led by Black lawmakers that is popular with speakers of Zulu, the language most widely used in South African homes. According to the deal, Inkatha will oversee committees in Parliament.

Inkatha wants chiefs and other traditional leaders to play a greater role in government, and to redistribute land to Black South Africans, but it has proposed a more conservative approach than M.K. and the Economic Freedom Fighters.

The notion of Inkatha’s working alongside the A.N.C. carries some symbolic significance.

In the turbulent years toward the end of apartheid, fighting between supporters of the A.N.C. and Inkatha left thousands dead and threatened to derail the 1994 election. “This presents an important opportunity between the two political parties to heal the wounds of the past,” said Velenkosini Hlabisa, Inkatha’s leader.

Fikile Mbalula, a top A.N.C. official, went to great lengths to disavow the narrative that working with the Democratic Alliance, or any other party, would betray the A.N.C.’s core historical commitment to liberate South Africa’s Black majority.

He pointed out that in the first government of national unity in 1994, the A.N.C. teamed up with the National Party, the leader of the apartheid government.

“We went into government with people who took us to jail,” Mr. Mbalula said. “Did we die? We didn’t. Did we survive that moment? We did.”

world