KABUL, Afghanistan -- Afghanistan's two rival candidates reached a breakthrough agreement Saturday to a complete audit of their contested presidential election and, whoever the victor, a national unity government.

The deal, brokered by U.S. Secretary of State John Kerry, offers a path out of what threatened to be a debilitating political crisis for Afghanistan, with both candidates claiming victory and talking of setting up competing governments.

Such a scenario could have dangerously split the fragile country's government and security forces at a time the U.S. is pulling out most of its troops and the Taliban continues to wage a fierce insurgency.

Instead, former Finance Minister Ashraf Ghani and former Foreign Minister Abdullah Abdullah agreed to abide by a 100 per cent, internationally supervised audit of all 8 million ballots in the presidential election. They vowed to form a national unity government once the results are announced, presumably one that includes members of each side.

Kerry, who conducted shuttle diplomacy between the two candidates late into the night Friday and Saturday, warned that much work still remained.

"This will be still a difficult road because there are important obligations required and difficult decisions to be made," Kerry told reporters after briefing Afghanistan's current president, Hamid Karzai, shortly after midnight.

The audit, which comes after widespread fraud allegations, is expected to take several weeks, beginning with the ballot boxes in the capital of Kabul.

Boxes from the provinces will be flown to the capital by helicopter by U.S. and international forces and examined on a rolling basis. Representatives from each campaign as well as international observers will oversee the review, and the candidate with the most votes will be declared the winner and become president.

Both candidates agreed to respect the result, and the winner would immediately form a national unity government. The inauguration, which had been scheduled for Aug. 2, would be postponed, with Karzai staying on a little longer as president.

Abdullah said the election created "serious challenges." But he praised Ghani for working toward the accord on the the audit and the unity government.

Ghani returned the compliments, lauding his competitor's patriotism and commitment to a dialogue that promotes national unity.

"Stability is the desire of everyone," Ghani said. "Our aim is simple: We've committed to the most thorough audit" in history. Such a process would remove any ambiguity about the result, he added.

Abdullah and Ghani spoke first in English, then in Dari. Ghani also spoke in Pashto. When they were done, they shook hands and hugged. Kerry later joined them as they raised their arms in triumph hand-in-hand.

The announcement came as a relief to a country on edge and worried about how the election dispute would resolve itself. Both the full audit and the agreement to form a unity government drew praise from television commentators immediately after the speeches.

The prolonged uncertainty about the outcome of the election had jeopardized a central plank of President Barack Obama's strategy to leave behind a stable state after the withdrawal of most U.S. troops at year's end.

Preliminary runoff results, released earlier this week against U.S. wishes, suggested a massive turnaround in favour of Ghani, the onetime World Bank economist. He had lagged significantly behind Abdullah in first-round voting.

Abdullah, a top leader of the Northern Alliance that battled the Taliban before the U.S.-led invasion in 2001, claimed massive ballot-stuffing. He was runner-up to Karzai in a fraud-riddled 2009 presidential vote before he pulled out of that runoff, and many of his supporters see him being cheated for a second time. Some, powerful warlords included, have spoken of establishing a "parallel government."

Kerry and Karzai discussed the deal past midnight Saturday. When they emerged early Sunday, the Afghan leader endorsed the outcome.

Speaking alongside Karzai at the Presidential Palace, Kerry said the democracy springing up in Afghanistan "deserved its full bloom." He offered robust U.S. support to ensure the deal holds.

The UN chief in Afghanistan, Jan Kubis, who will direct much of the technical aspects of the audit, delivered his strongest praise for Kerry. He said Kerry's work wasn't typical diplomacy but almost a "miracle."

Extended instability would have immediate consequences for Afghanistan. If no process had been established and both Ghani and Abdullah attempted to seize power, the government and security forces could have split along ethnic and regional lines. The winner amid such chaos could be the Taliban, whose battle against the government persists despite the United States spending hundreds of billions of dollars and losing more than 2,000 lives since invading the country after the Sept. 11, 2001, terrorist attacks.

The Taliban have intensified their spring offensive in a bid to undermine the Western-backed government. Saturday's breakthrough came after two roadside bombs killed at least 10 people, authorities said. The Taliban was blamed for the larger attack in Kandahar province.

Kerry repeatedly stressed in his mediation that Washington isn't taking sides.

Kubis and other officials said the talks in Kabul focused on the technical particulars of the UN audit. Kerry spent significant time hammering home the point that each side must come together at the end of the contest for the good of the country.

With Iraq wracked by an extremist Sunni rebellion, the Obama administration moved quickly to ensure Afghanistan's political instability also didn't break out into violence. A prolonged crisis also could have had other security implications for Washington.

Both Ghani and Abdullah have vowed to seal a bilateral security pact with the U.S. that Karzai has refused to sign.

The United States says it needs the legal guarantees in order to leave behind some 10,000 troops in Afghanistan next year. If the pact isn't finalized, U.S. officials say they may have to pull out all American forces, an undesired scenario that played out three years ago in Iraq.