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 hard journey on a long road begins now and the work must continue," added Mr Kerry, who played an instrumental role in bringing together the two sides.
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.
Mr Kiir and Mr 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.
Alastair Leithead reports on rape and murder in Bentiu
Leading mediator Seyoum Mesfin, from the regional Igad bloc, congratulated Mr 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.
The violence began when President Kiir accused his sacked deputy Mr Machar of plotting a coup.
Mr Machar denied the allegation, but then marshalled a rebel army to fight the government.
The battle assumed ethnic overtones, with Mr Machar relying heavily on fighters from his Nuer ethnic group and Mr 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.
South Sudan's President Salva Kiir and rebel leader Riek Machar have agreed a peace deal after a five-month conflict.