The deadlock over last month's run-off vote to succeed outgoing President Hamid Karzai plunged Afghanistan into crisis and dented US hopes of a smooth transfer of power as Washington seeks to withdraw all its troops by late 2016.
Preliminary results of the second-round vote put Ashraf Ghani in the lead, but Abdullah Abdullah -- who has already once lost a presidential bid -- declared himself the true winner, saying massive fraud robbed him of victory.
But after two days of intense talks brokered by Kerry, the rivals reached an agreement late on Saturday evening.
"Both candidates have committed to participate in and stand by the results of the largest most possible audit. Every single ballot that was cast will be audited, all eight million," Kerry told reporters.
More From This Section
"The winner will serve as president and will immediately form a government of national unity."
The deal went further than a UN proposal made late on Thursday to audit just over 8,000 polling stations where suspicions of ballot-stuffing had been raised -- around 44 percent of the total votes cast.
The chief US diplomat said the audit would be "conducted in accordance with the highest international standards" and would take "a number of weeks".
He said the UN mission in Afghanistan had asked for the presidential inauguration date, scheduled for August 2, to be postponed.