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 consented to abide by a 100 percent, internationally supervised audit of all 8 million ballots in the presidential election and a national unity government once the results are announced.
The audit is expected to take a "number of weeks" and would begin with the ballot boxes in Kabul. Ballot boxes from the provinces are to flown by helicopter to the capital by U.S. and international forces and examined on rolling basis. Observers from each campaign as well as international observers will be involved in the oversight of the review, and the candidate with the most votes would be declared the winner and become president.
Both candidates agreed to respect the result, and the winner would immediately form a government of national unity. The inauguration, which had been scheduled for Aug. 2, would be postponed.
Abdullah, who spoke first at the news conference announcing the breakthrough, said the election created "serious challenges."
But he praised Ghani for contributing to the agreement over how the audit would be conducted and the framework for a unity government to be established once the victor is determined.
Ghani returned the compliments to Abdullah, lauding his competitor's patriotism and commitment to a dialogue that promotes national unity.
"Stability is the desire of everyone," he 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.
Ghani noted Karzai reluctantly agreed to stay on as president until the new government formed for the good of the country.
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 has 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 favor of the onetime World Bank economist Ghani, who 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 met for the second day with Ghani and Abdullah after discussions Friday proved inconclusive, even though both candidates have acknowledged fraud in the election and agreed in principle to a U.N. investigation. He also met with current Afghan President Hamid Karzai and the U.N. chief in Afghanistan, Jan Kubis.
The bitter dispute over who is Karzai's rightful successor has alarmed Afghanistan's U.S. and Western benefactors, creating a political crisis that risks undermining more than a decade of efforts to build an Afghan government capable of fighting the Taliban on its own and snuffing out terrorist groups like al-Qaida.
Extended instability would have more 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.
And the winner amid all the 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 as foreign combat troops prepare to withdraw from the country by the end of the year. The breakthrough announcement came after two roadside bombs killed at least 10 people Saturday, authorities said. The Taliban was blamed for the larger attack in Kandahar province.
Kerry repeatedly has stressed that Washington isn't taking sides. Senior U.S. officials said the talks in Kabul had focused on the technical particulars of a U.N. audit and hammered home the point that whoever proves the winner, the new government must bridge Afghanistan's many ethnic and regional divides.
However, one of the officials said only the "beginnings of conversations" had occurred over the first day, and offered no prediction of any breakthrough. The officials briefed reporters on condition of anonymity because they weren't authorized to be quoted while the talks were ongoing.
Ghani and Abdullah had differed on some of the fine points of the U.N.'s audit plan. Abdullah, for example, wanted more voting districts examined. Other questions center on who would be included among the investigators, where they'd travel and how they'd assess the level of fraud.
With Iraq wracked by insurgency, Afghanistan's postelection chaos is posing a new challenge to Obama's effort to leave behind two secure governments while ending America's long wars.
Both Ghani and Abdullah have vowed to sign a bilateral security pact with Washington, which says it needs the legal guarantees in order to leave behind some 10,000 troops in Afghanistan after most of the American military pulls out over the next five months.
Karzai has refused to sign a U.S.-Afghan agreement, leaving it in the hands of his successor.