The deal was struck in neighbouring Ethiopia by representatives of President Salva Kiir and delegates loyal to sacked vice-president turned rebel leader Riek Machar. It was greeted by cheers from regional mediators and diplomats, and was welcomed in Washington.

President Barack Obama welcomed the ceasefire but said leaders needed to work to resolve the underlying causes of the conflict, and must quickly release political detainees.

"South Sudan's leaders must demonstrate their sustained commitment to a peaceful resolution of the crisis," Obama said in a statement.

"They have an obligation to ensure that the lives of their people and future of their young country are not further marred by continued violence, and that individuals who have committed atrocities are held to account."

The pact, which marks the first significant breakthrough since a power struggle between Kiir and Machar turned to violence on 15 December, is expected to be implemented within 24 hours of the signing, mediators said. The government also reportedly agreed to release 11 officials close to Machar from detention, a major point of dispute, although no time frame was given.

Taban Deng, head of the rebel delegation, said he hoped the deal would "pave the way for a serious national political dialogue aiming at reaching a lasting peace in South Sudan".

Government negotiator Nhial Deng Nhial said the talks, which began in Addis Ababa three weeks ago, were "not easy". "We hope to be able to make haste towards an agreement that will end bloodshed," he was quoted as saying by AFP.

But it remains to be seen whether all fighters in South Sudan, a patchwork of rival militias with competing loyalties, will abide by the outcome.

"What worries us is whether the agreement on the cessation of hostilities will stick [and] the capacity of the rebel group … to stop fighting," Deng said. "We would like to take this opportunity to urge the rebel group to heed the voice of reason and abandon the quest for political power through violence."

His concern was echoed by a spokesman for the South Sudan's military, who cautioned that a group known as the "white army" may not want peace.

"Riek Machar has been using that force to fight the SPLA [Sudan People's Liberation Army], so we have to see what will happen," Colonel Philip Aguer told the Associated Press. "Civilians, innocents are dying, so it is good for the people of South Sudan to have peace."

After initial clashes broke out in the presidential guard five weeks ago, the conflict rapidly escalated into war between the regular army, backed by Ugandan troops, and breakaway units and other militia. It also took on an ethnic character as members of Kiir's Dinka tribe clashed with Machar's Nuer group. Some analysts say as many as 10,000 people have died, while close to half a million have been displaced.

The fighting has also affected South Sudan's oil industry, after technical workers fled and rebel fighters took control for while of the fields.

Earlier this week government forces recaptured the town of Malakal in the oil-producing Upper Nile state and the last major settlement under rebel control. Large numbers of rebel forces, however, are still massed in rural areas and smaller towns.

"To the parties, we say, 'Enough,'" Alexander Rondos, the EU special representative for the Horn of Africa, said at the signing of the deal on Thursday. "The killing must end now. The displaced must be able to return to their homes."

The US, which spent billions of dollars helping South Sudan achieve independence in July 2011, also welcomed the deal. The White House spokesman Jay Carney said: "This is a first critical step in ending the violence … we expect both parties to fully and swiftly implement the agreement.

"The United States urges both sides to build on this momentum by moving swiftly to an inclusive political dialogue."

Others sounded a note of caution. Seyoum Mesfin, the chief mediator in Addis Ababa, told the ceremony: "The crisis that gripped South Sudan is a mere manifestation of the challenges that face the young and fledgling state.

"I believe that the postwar challenges will be greater than the war itself. The process will be ... unpredictable and delicate."

There was also scepticism on the streets of Juba, the South Sudanese capital. "It can solve some of the immediate problems but not all the problems," Samuel Kuir Chok, 31, told Reuters. "I'm not optimistic … because this guy [Machar)] wants to be president at all costs."

The Enough Project, a US-based advocacy group, said Thursday's deal is only the first step on a long road to a sustainable peace.

"If an inclusive peace process is not constructed that seeks to address root causes, the conflict will continue, with deadly consequences," said John Prendergast, the group's co-founder, adding it was "far from guaranteed" that all combatants would lay down arms just because a deal was signed in Ethiopia.

José Barahona, Oxfam's country director for South Sudan, added: "The world's newest nation, plagued by conflict for the past month, has today been given a second chance. With the cessation of hostilities, the focus must now be on rapid reconciliation to aide a fast recovery and set South Sudan on a clear path to development."

The UK foreign secretary, William Hague, welcomed the agreement, and said both sides must now ensure their forces stop fighting within 24 hours, as stipulated.

He said: "The brutal violence of the last month has led to countless deaths and caused thousands of innocent people to endure unimaginable suffering.

"The government and opposition must ensure that their forces implement the agreement immediately and in full.

"It is now vital that both sides work to heal the divisions that led to this conflict, and to strengthen governance in South Sudan. A genuinely inclusive process of national reconciliation is now needed, to give the people of South Sudan confidence that such violence can never reoccur. The UK is ready to lend its full support to these efforts in South Sudan.

"I fully support the African Union's decision to establish a commission of inquiry into alleged crimes committed during the conflict, and call on all sides to co-operate with it to ensure that those responsible for abuses are held accountable."".