NAIROBI — Warring sides in South Sudan formed a "unity government" Saturday with rebel leader Riek Machar as vice president, launching the most serious bid in years toward ending a merciless civil war that has killed at least 400,000 people and left millions homeless.

The agreement between Machar and South Sudanese President Salva Kiir comes after two failed attempts that resulted in a return to war.

International pressure had mounted on the two leaders after deadlines to reach a peace agreement passed over the past year. But key concessions were made in the past week, clearing the way for a deal.

“This action signifies the official end of the war, and we can now declare a new dawn in South Sudan,” Kiir said at the ceremony. “Peace has come to stay, not to be shaken ever again in this nation.”

Almost as many people have died in South Sudan’s civil war as in Syria’s, and in less time.

Conflict has plunged parts of the country into famine and driven more than 2.2 million people into neighboring countries, while 1.4 million in South Sudan have been left homeless and 190,000 are living under direct United Nations protection.

“This agreement marks a turning point in our history,” South Sudan’s foreign minister, Awut Deng Acuil, said in a telephone interview. “The suffering of our people is going to end. We will facilitate the return of people from neighboring countries.”

The agreement hinged on two key sticking points.

The first was Machar’s demand that South Sudan reduce its number of states from 32 to 10, contending that new borders had essentially been gerrymandered in a way that favored the Dinka ethnic group of Kiir.

Kiir insisted that Machar not be allowed to bring his personal security forces into the capital, Juba, fearing a repeat of the violence that rocked the city in July 2016 after the last peace deal failed.

“This was the only foreseeable path forward. It is a momentous day,” said Alan Boswell, an analyst focusing on South Sudan at the International Crisis Group, who was in Juba for the ceremony Saturday and recently returned from areas where Kiir’s and Machar’s security forces were attempting to integrate into one unified army.

“In other ways, though, it is a crawling step forward and doesn’t drastically change the situation in the country,” he added. “South Sudan isn’t going to emerge from being a failed state overnight. It will take the work of generations to put its shattered pieces back together — even to get it back to where it was at independence.”

South Sudan became the world’s newest country in 2011 after a bloody conflict to secede from Sudan, now its neighbor to the north.

In 2013, the rivalry between Kiir and Machar, who was also vice president at that time, turned violent and spiraled into a full-blown war. Countless atrocities committed by both sides have been documented by journalists, human rights observers and the United Nations.

A U.N. report released just days ago details how despite moves toward peace, war crimes are probably still being committed by both sides.

“The people of South Sudan have been deliberately starved in different parts of the country for ethnic and political reasons, and sexual violence against women and men as a weapon of war is ongoing,” the authors of the report said Thursday. Their report also details the siphoning of millions of dollars from government coffers by officials in Kiir’s administration.

In recent months, regional leaders, especially from South Sudan’s neighbors — Uganda, Ethi­o­pia and Sudan — met with Kiir and Machar to iron out the agreement.

The United States, which played a pivotal role in South Sudan’s independence, took a more punitive approach, imposing sanctions on members of Kiir’s government in an effort to move the process forward.

Even Pope Francis got involved last year, kissing the feet of Kiir and Machar in a moment that was reproduced in photos that the two leaders were presented with at the start of Saturday’s ceremony in Juba.

“While much work remains to be done, this is an important milestone in the path to peace,” the U.S. Embassy said in a message of congratulations.

Deng, the foreign minister, said South Sudan and the United States “are not enemies” and that “relations have to be normalized to ensure South Sudan can recover fully.”

Unlike past agreements between Kiir and Machar, the current one is expected to last, even if contentions remain.

Machar has accused Kiir of carving out an oil-rich zone from his home state and turning it into a federally administered zone, effectively keeping it under his control, for instance. Kiir also fired all 32 state governors and their staffs, some of whom control their own militias.

“There will be losers from this, but I wouldn’t go so far as to say they will be spoilers to the peace,” Boswell said. “Still, no one should doubt how painful this is for everyone involved, including Kiir.”

Kiir and Machar will now work together to raise money for recovery efforts, meld their security forces into a single army and set the stage for national elections, which would still be years away.

They will probably face each other as candidates for president.