"All parties have agreed on a permanent ceasefire within 72 hours of signing the Khartoum Document," Sudan's Foreign Minister Al-Dierdiry Ahmed announced following talks in the Sudanese capital.
Kiir and Machar then signed the document in the presence of Sudanese President Omar al-Bashir.
"This day was expected by our people in South Sudan and it has now come," Kiir said after the signing of the agreement.
Machar said the ceasefire must finally lead to the "ending of the war".
The latest push for peace in South Sudan comes as part of a fresh bid launched by East African leaders and with the two factions facing a looming deadline to avert UN sanctions.
Several previous ceasefire agreements have been violated.
"We offer this agreement as a gift to South Sudanese citizens," Bashir said. "This agreement says that peace has started to return to South Sudan."
The Khartoum negotiations came after a round of talks brokered by Ethiopian Prime Minister Abiy Ahmed last week in Addis Ababa faltered.
On arriving in Khartoum, Kiir and Machar expressed their readiness to talk peace as the dialogue opened in the presence of Bashir and Ugandan President Yoweri Museveni.
South Sudan's war, which has killed tens of thousands of people and displaced about four million, broke out in December 2013 when Kiir accused his then-deputy Machar of plotting a coup, dashing the optimism that accompanied independence from Sudan just two years earlier. (AFP)