U.S. Secretary of State John Kerry (L) and Afghanistan's presidential candidates Ashraf Ghani (C) and Abdullah Abdullah hold their arms in the air together after announcing a deal for the auditing of all Afghan election votes at the United Nations Compund in Kabul, July 12, 2014. The two rival candidates in Afghanistan's presidential election agreed to abide by the results of a U.N.-supervised recount of the entire poll to settle their dispute over the outcome, U.S. Secretary of State John Kerry said after talks with both men. REUTERS/Jim Bourg

U.S. Secretary of State John Kerry said Saturday he worked out an agreement with both Afghan presidential candidates to honor the results of a comprehensive audit to resolve the controversy-plagued elections.

Kerry met with both candidates, Abdullah Abdullah and Ashraf Ghani Ahmadzai, during the weekend. Both claim victory in the runoff election earlier this year. Ghani is far ahead in the preliminary poll results, but Abdullah alleges massive voter fraud and has refused to recognize the results.

Kerry made a last-minute stop in Afghanistan after a trip to China to intervene in the crisis that threatened to derail the country’s first democratic handover of the presidency. The audit will start within 24 hours.

“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 8 million,” Kerry said.

Easing fears Abdullah supporters would seek a “parallel” government, both Abdullah and Ghani agreed to a unity government. Abdullah, a former foreign minister nearly won enough votes in the general election to avoid a runoff with Ghani, a former finance minister. Abdullah is Tajik and expressed suspicion Ghani, a Pashtun (the majority ethnic group in Afghanistan) got help from the outgoing President Hamid Karzai, also a Pashtun.

The tension threatened to break up the country along ethnic lines, sending it back into a civil war after years of relative stability following the U.S. invasion in 2001.

“From day one, when we submitted our nominations, our commitment has been to an inclusive government; a government that could represent all of Afghans and serve every Afghan citizen in the manner that every Afghan deserves according to the constitution,” Ghani said.

The goal of the audit “is to ensure that the election process enjoys the integrity and the legitimacy that the people of Afghanistan and the world will believe in,” Ghani said earlier.

A stable government will allow the U.S. to withdraw all but 10,000 American troops from Afghanistan by year's end.