Afghanistan's two presidential election candidates have agreed to an audit in an attempt to resolve a dispute over the result of last month's vote, US Secretary of State John Kerry has said.
Both contenders, Abdullah Abdullah and Ashraf Ghani, have agreed to abide by the result.
The review of all 8m ballots would begin within 24 hours, Mr Kerry said.
Preliminary results suggest Mr Ghani has won, but both candidate accuse the other of electoral fraud.
Results announced by Afghanistan's election officials give Mr Ghani 56.44% of votes in the 14 June run-off, with Mr Abdullah gaining 43.45%.
The results were markedly different from those achieved in the first round of voting, held in April, when Mr Ghani came a distant second.
Afghanistan's current President Hamid Karzai, who came to office after the US-led overthrow of the Taliban, is stepping down after more than 10 years.
He has welcomed the audit and agreed to delay his successor's inauguration to allow time for the review, which is expected to take several weeks.
At a news conference with the two candidates, Mr Kerry said that every single ballot would be audited.
"This is the strongest possible signal by both candidates of a desire to restore legitimacy to the process and to Afghan democracy," he said.
Ballots in Kabul would be checked first, and the International Security and Assistance Force (Isaf) would bring the rest from the provinces, he added.
The deal was hammered out in two days of intense talks and goes much further than many Afghans had expected, the BBC's Karen Allen in Kabul says.
It is a measure of how much international political, military and financial commitment rests on ensuring Afghanistan does not slip backwards into violence, our correspondent adds.
At the press conference, Mr Ghani described the move as the "most comprehensive audit in the history of elections... such a process would remove any ambiguity about the result".
Meanwhile, Mr Abdullah said the two sides had reached a "technical and political agreement".
"I hope this is for the benefit of Afghan people," he added.
'Difficult road ahead'
Counting of votes is to begin early on Sunday morning, with the ballot boxes being moved under tight security to counting locations. The votes will be under constant guard until new results are disclosed.
The framework for a unity government may be being sketched out, but the details about the possible division of power are unclear.
Mr Kerry arrived on Friday for a hastily arranged visit, amid concerns at reports that Mr Abdullah was planning a "parallel government".
Speaking on Saturday as the audit was announced, Mr Kerry warned of "a difficult road" ahead because of "important obligations required and difficult decisions to be made".
He added: "We hope that the promise of the next weeks will deliver the authenticity and credibility that the people of Afghanistan deserve."
Analysts say Mr Ghani and Mr Abdullah both command strong support and any split in the government or armed forces could undermine the country's stability.