* Ceasefire to be implemented in 24 hours
* Legal process for Juba detainees to continue
* Pact calls for withdrawal of "allied forces"
South Sudan's government and rebels signed a ceasefire on Thursday to end more than five weeks of fighting that divided Africa's newest nation and brought it to the brink of civil war.
Fighting between troops loyal to President Salva Kiir and those backing the vice president he sacked in July, Riek Machar, erupted in mid-December.
Thousands of people have been killed and more than half a million people have fled their homes, prompting the regional grouping of nations, IGAD, to initiate peace talks.
The pact is expected to be implemented within 24 hours of the signing, mediators said.
But making the ceasefire hold could test Machar, whose forces include loyalists as well as more autonomous groups battling the centrally controlled government forces.
"The crisis that gripped South Sudan is a mere manifestation of the challenges that face the young and fledgling state," Seyoum Mesfin, IGAD's chief mediator, told the signing ceremony.
"I believe that the postwar challenges will be greater than the war itself. The process will be ... unpredictable and delicate."
South Sudan's defence minister, Kuol Manyang Juuk, told Reuters on Jan. 17 before the deal was reached that Machar did not have enough control to make a ceasefire stick in the oil-producing nation, one of Africa's poorest.
"To the parties, we say: Enough! The killing must end now. The displaced must be able to return to their homes," said Alexander Rondos, the European Union's special representative for the Horn of Africa, at the signing event.
The conflict has turned along ethnic faultlines, pitting Machar's Nuer against Kiir's Dinka people. Several other communities have also taken up weapons. Analysts say the ceasefire does not resolve the broader power struggle.
"It is only the first step to allow space and time for a more substantive political dialogue to take place," said Douglas Johnson, a historian and author.
Both sides had said several times since talks began at the start of January they were close to a deal, but disagreements had pushed back a signing. Meanwhile, fighting raged, with the government retaking major towns from rebel forces.
"This deal does not provide answers to South Sudan's current problems. We need a comprehensive political deal," said one rebel official in the Ethiopian capital.
"We are only signing because we, and they, are under pressure."
Ordinary people in South Sudan's capital Juba were also sceptical the ceasefire would swiftly end the political rivalry that underpinned the fighting.
"It can solve some of the immediate problems but not all the problems," said 31-year-old Samuel Kuir Chok. "I'm not optimistic ... because this guy (Machar) wants to be president at all costs."
The ceasefire was accompanied by an agreement on the "question of detainees". Rebels had demanded the release of 11 of Machar's allies, detained by the government and accused of attempting a coup.
Seyoum, the chief mediator, told reporters the deal provided for the 11 to eventually participate in the peace process - but that they must first face due process of law.
Shortly before the signing, rebel spokesman Mabior Garang said freeing the detainees was "not so much of a demand since everyone recognises the need for their release".
The rebels have also demanded that Uganda, which openly admitted to helping Kiir's forces in combat, leave South Sudan.
Diplomats at the talks had said the deal would call for an end to "involvement by foreign forces", but Hussein Mar Nyot, the spokesman for Machar's delegation, said it called for a 'withdrawal of allied forces invited by both sides'".
South Sudan won its independence from Sudan in 2011 after decades of conflict between the northern and southern Sudanese.