Riek Machar was accused of plotting a coup, which he denied
South Sudan's President Salva Kiir and rebel leader Riek Machar have agreed a peace deal after a five-month conflict.
The deal calls for an immediate truce and the formation of a transitional government ahead of the drafting of a new constitution and new elections.
The conflict in the world's newest state has left thousands dead and more than one million homeless.
US Secretary of State John Kerry said the deal "could mark a breakthrough for the future of South Sudan".
"The hard journey on a long road begins now and the work must continue," added Kerry, who had played an instrumental role in bringing together the two sides in the conflict.
The UN has accused both the South Sudanese government and the rebels of crimes against humanity, including mass killings and gang-rape.
The rivals signed the deal in the Ethiopian capital, Addis Ababa late on Friday, after their first face-to-face meeting since the hostilities began.
The BBC's Emmanuel Igunza in Addis Ababa says the agreement calls for a cessation of hostilities within 24 hours of the signing. A permanent ceasefire will then be worked on.
Kiir and Machar are to issue immediate orders for troops to end combat and to allow in humanitarian aid.
It was not immediately clear who would form the transitional administration.
The deal was also signed by Ethiopian Prime Minister Hailemariam Desalegn, who hosted the talks.
Leading mediator Seyoum Mesfin, from the regional Igad bloc, congratulated Kiir and Mr Machar for "ending the war".
However, African Union official Smail Chergui warned that "given the current crisis, the restoration of peace in South Sudan will not be easy".
A UN report released on Thursday said that "widespread and systematic" atrocities had been carried out by both sides in homes, hospitals, mosques, churches and UN compounds.
It called for those responsible to be held accountable.
An estimated five million people are in need of aid, the UN says.
A cessation of hostilities deal was signed by both sides in January but failed to bring an end to the violence.
The violence began when President Kiir accused his sacked deputy Machar, of plotting a coup.
Machar denied the allegation, but then marshalled a rebel army to fight the government.
The BBC outlines the background to South Sudan's crisis - in 60 seconds.The battle assumed ethnic overtones, with Machar relying heavily on fighters from his Nuer ethnic group and Kiir from his Dinka community.
The UN has about 8,500 peacekeepers in South Sudan. However, they have struggled to contain the conflict.
South Sudan gained independence in 2011, breaking away from Sudan after decades of conflict between rebels and the Khartoum government.
It remains one of the world's poorest countries.