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.
A ceasefire agreed in January collapsed within days, with both sides accusing each other of restarting the fighting.
US Secretary of State John Kerry said Friday’s agreement “could mark a breakthrough for the future of South Sudan”.
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 agreement calls for a cessation of hostilities within 24 hours of the signing. A permanent ceasefire will then be worked on.
Salva Kiir and Riek 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 PM Hailemariam Desalegn, who hosted the talks.
Leading mediator Seyoum Mesfin, from the regional Igad bloc, congratulated Salva Kiir and Riek 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.
The violence began when President Salva Kiir accused his sacked deputy Riek Machar of plotting a coup.
Riek Machar denied the allegation, but then marshaled a rebel army to fight the government.
The battle assumed ethnic overtones, with Riek Machar relying heavily on fighters from his Nuer ethnic group and Salva 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.
zn2r4TqckkMImage source: Wikimedia Commons President-elect Donald Trump celebrated his election victory at the Ultimate Fighting…
Millions of voters across the US chose to return Donald Trump to the White House…
Donald Trump declares victory in the US election as he addresses jubilant supporters in Florida.…
Stocks around the world are rising as Donald Trump appears to be on the cusp…
Donald Trump has won Pennsylvania, North Carolina and Georgia and taken a lead over Kamala…
Quincy Jones, the celebrated musician and producer who worked with Michael Jackson, Frank Sinatra, Ray…