You are not permitted to download, save or email this image. Visit image gallery to purchase the image.
The recount was scheduled to begin within 24 hours, but was likely to take several weeks, meaning that a presidential inauguration due on Aug. 2 will be postponed.
The deadlock over the June 14 election run-off between the leading contenders, Abdullah Abdullah and Ashraf Ghani, has raised concerns about a smooth transition of power in Afghanistan just as U.S.-led troops are leaving.
"Both candidates have committed to participate in and abide by the results of the largest and most comprehensive audit. Every single ballot that was cast will be audited," Kerry told a joint news conference with both candidates, held just before midnight after talks dragged for two days.
"This is the strongest possible signal by both candidates of the desire to restore legitimacy to the process."
The stakes are high for the United States. Washington hopes to settle the dispute quickly so it can sign a crucial security pact with Afghanistan allowing a small contingent of U.S. troops to stay in the country beyond this year.
Unlike incumbent Hamid Karzai, both Abdullah and Ghani have promised to sign the deal promptly, but the protracted standoff over the vote has delayed the process.
Ghani and Abdullah have been locked in an acrimonious dispute since the June 14 run-off, both effectively claiming victory and Abdullah refusing to accept the outcome.
They have not met each other in person since the vote but Kerry managed to bring both men together at a U.N. compound in central Kabul to give an unprecedented joint news conference.
"This audit will be conducted in accordance with the highest international standards," said Kerry, flanked by both candidates in a packed news conference. As he announced the total recount of the votes, many in the audience gasped.
"The auditing will be internationally supervised in a manner proposed by the UN assistance mission, the candidates campaigns will each provide joint oversight of the audit."
In a show of unity after months of bitter bickering, Ghani kissed Abdullah on the cheek after addressing reporters.
"Since we have agreed to a 100 percent audit of ballots I request from President Karzai to postpone inauguration of new government," Abdullah said.
Preliminary results from the run-off vote put Ghani, a former World Bank official, in the lead by almost one million votes. Abdullah rejected the result, claiming widespread fraud and calling the outcome a "coup" against the Afghan people.
In comments to reporters on Friday, Kerry said the transition to a self-reliant state hung in the balance unless the legitimacy of the election could be restored.
Washington has warned of repercussions if either side declares victory and tries to grab power.
The United States is in the process of withdrawing its forces from Afghanistan after 12 years of fighting Taliban insurgents, but the country remains dependent on foreign aid. The U.S. is Afghanistan's biggest foreign donor. (Writing by Maria Golovnina in Kabul, editing by Kevin Liffey and David Evans)