KABUL, Afghanistan -- After nearly 12 hours of often tense negotiations, Secretary of State John Kerry announced Saturday that Afghanistan would audit all 8 million votes cast in a runoff presidential election last month as part of a deal to end a tense power struggle between the top two candidates.
The audit, intended to resolve allegations of widespread fraud, is to begin almost immediately, he said, and will be supervised by international monitors. Its results will be binding, and the winner will go on to head a national unity government whose shape has yet to be finalized.
"Every single ballot that was cast will be audited," Mr. Kerry said at a news conference where he was flanked by the two candidates, Abdullah Abdullah and Ashraf Ghani Ahmadzai, who endorsed the agreement.
The breakthrough came on the second day of a visit that Mr. Kerry hastily arranged to try to prevent Afghanistan's first democratic transfer of power from collapsing. Both Mr. Abdullah and Mr. Ahmadzai had said the election was marred by fraud, but both had also claimed victory.
Tensions were heightened Tuesday when Mr. Abdullah threatened to form his own government following the release of preliminary results from the runoff, raising the possibility of an ethnically and regionally divided Afghanistan just as U.S. troops prepare to complete their combat mission.
Mr. Kerry spent Friday largely listening to the proposals of the Abdullah and Ahmadzai camps and meeting with President Hamid Karzai.
And then Saturday, shortly before 9 a.m., he began what would turn out to be an entire day of shuttling between meeting rooms at the U.S. Embassy, where each candidate had gathered with a handful of advisers. He also met with Mr. Karzai at the presidential palace in the afternoon.
By the time they announced the deal, Mr. Kerry looked like a man who had put in a long day's work. Mr. Abdullah and Mr. Ahmadzai looked similarly worn out by the marathon negotiations, which had achieved what they alone could not in the weeks since the June 14 runoff.
Mr. Kerry said the audit would begin within 24 hours and take several weeks, and Mr. Karzai indicated that he would agree to postpone the inauguration, though he urged that the auditors try to complete their work in time to meet the scheduled date of Aug. 2.
He did not set a new date, only saying it would take place after the audit.
Mr. Karzai seemed comfortable with the outcome, something of a turnaround, as he had previously refused to allow international "interference" in the elections.
If it is implemented successfully, the deal will be an accomplishment for Mr. Kerry, who has long played a troubleshooting role on Afghanistan.
When he was a senator and chairman of the Senate Foreign Relations Committee, Mr. Kerry persuaded Mr. Karzai to accept a runoff in the contested 2009 presidential election. In March 2013, Mr. Kerry went to Kabul to rebuild relations with Mr. Karzai after he publicly accused the United States of wanting to destabilize the country.
Not all of Mr. Kerry's efforts on Afghanistan have held up. In November 2013, Mr. Kerry announced that the United States and Afghanistan had completed the wording of a bilateral security agreement that would establish a legal basis for keeping U.S. forces in Afghanistan after 2014. Mr. Karzai later refused to sign the agreement, though Mr. Abdullah and Mr. Ahmadzai have each indicated that they would do so shortly after assuming office.
To ensure security -- and to give the candidates confidence that no new vote tampering was taking place during the audit -- the NATO-led military coalition in Afghanistan is to fly ballot boxes that are currently in the provinces to Kabul, the capital, and work with Afghan forces to provide security for the boxes once they arrive. United Nations and other international observers will watch the entire process.
Mr. Kerry had to seek not only agreement by the two candidates, but also acceptance by Mr. Karzai, who will step down after 13 years in power and has by all accounts remained a powerful presence behind the scenes.
In the first round of voting April 5, Mr. Abdullah emerged the top vote-getter against 11 other candidates, with 45 percent to Mr. Ahmadzai's 31 percent. Because neither won more than 50 percent of the vote, a required runoff between the two was held on June 14.
Preliminary results from the runoff show Mr. Ahmadzai leaping ahead with 56 percent of the vote, and Mr. Abdullah with 44 percent. The turnout in the runoff also increased by more than a million voters, to 8 million.
Mr. Abdullah's campaign has accused his opponents of conducting extensive institutional fraud, and has alleged that members of the Independent Election Commission, government officials and Ahmadzai supporters orchestrated large-scale ballot stuffing to enable Mr. Ahmadzai to win the presidency.
Mr. Ahmadzai's team has said that fraud took place on both sides and insists that his better showing in the runoff was the result of an energetic campaign to mobilize his fellow ethnic Pashtuns to vote for him.
The dispute, which has delayed results for nearly a month since the initial vote, comes amid a rise in violence around the country. Civilians are killed and wounded more frequently now in Afghanistan than at any point since the arrival of U.S. forces, the International Crisis Group said in a statement Saturday.
Seven members of the Afghan security forces were killed in clashes with the Taliban east of the capital in Laghman province Saturday. Two tribal elders and a family of eight were killed in three separate attacks in southern Afghanistan, electricity has been knocked out in the city of Kandahar, and a bomb killed two people in Jalalabad. The power struggle between the candidates had threatened to once again plunge the country into ethnic violence.