— Secretary of State John F. Kerry helped ease a major political crisis in Afghanistan on Saturday, persuading rival presidential candidates to agree to a full recount of votes cast in a runoff election marred by widespread fraud.

Emerging from at least 20 hours of talks over two days, Kerry said that United Nations and international observers, along with observers from each campaign, will preside over the inspection of all 8 million ballots, which international troops will transfer to Kabul from Afghanistan’s 34 provinces. The winner of the recount, which Kerry said could take “many weeks,” will then form a national consensus government.

The incumbent president, Hamid Karzai, has been asked to postpone his successor’s inauguration, which had been scheduled for Aug. 2, and has agreed to stay on as president until the new government is formed.

“Today, we’re here at a pivotal moment for Afghanistan and its democracy as it seeks to complete a historical transition,” Kerry said at a joint news conference Saturday night with the two candidates, ex-foreign minister Abdullah Abdullah and former World Bank executive Ashraf Ghani. The rivals shook hands and eventually hugged, each praising the other’s commitment to Afghanistan and democracy.

Kerry sounded a note of caution, however, warning that there is a long way to go before the political crisis is resolved.

U.S. Secretary of State John F. Kerry, left, says both of Afghanistan's presidential candidates are committed to abiding by the results of the “largest, most comprehensive audit” of the election runoff ballots possible. (Rahmat Gul/Associated Press)

“This job will not be done until Afghanistan’s leaders certify an election and honor the determination of millions of Afghans to make their voices heard,” he said.

As the news conference concluded, Abdullah and Ghani raised their hands, then grabbed Kerry’s and raised his, too.

Kerry said, “Well, we haven’t won yet.”

The note of wariness was echoed in a statement earlier Saturday from the Brussels-based International Crisis Group, which warned against missing the constitutionally mandated inauguration date. The fraud investigation should be completed by Aug. 2, it said, “so that the dangers of a leadership vacuum are avoided.”

The preliminary vote count alone took several weeks.

The lengthy dispute between Abdullah and Ghani over the June 14 poll had threatened Afghanistan’s first democratic transfer of power just months before foreign troops are to withdraw. Alarmed by the impasse, the Obama administration dispatched Kerry to Kabul to mediate.

U.S. officials had also threatened to withhold billions of dollars in aid if the two sides could not reach a compromise. The United States has invested hundreds of billions of dollars in Afghanistan since 2001, when U.S.-backed Afghan forces toppled the Taliban government, and it continues to prop up both the Afghan government and the country’s fledgling security forces.

Also at stake is a security agreement with the Afghan government to keep a contingent of U.S. troops in Afghanistan after 2014. Karzai has refused to sign an agreement. But both Abdullah and Ghani have pledged to adopt an accord that would allow U.S. forces to stay in Afghanistan in exchange for immunity for U.S. personnel from local prosecution.

The two campaigns have engaged in heated discussions in recent weeks to break the deadlock that many Afghans feared could eventually fracture the country along ethnic or territorial lines. Abdullah draws support from Tajiks in the north and west. Ghani, an ethnic Pashtun, is popular among the Pashtun populations in the east and south.

Immediately after the June 14 runoff, Abdullah accused officials at the Independent Election Commission (IEC) of helping to rig the vote for Ghani, who had trailed him in the first round in April but ended up with a 12-point lead in the runoff.

Abdullah’s team withdrew its observers from the process and this week threatened to declare a parallel government. At a rally in the capital Tuesday that drew thousands, Abdullah’s supporters declared their candidate the legitimate victor in the election, despite an acknowledgment by IEC chief Ahmad Yusuf Nuristani that the election had been damaged by vote-tampering.

On Saturday, Kerry also stressed that the United States does not favor one candidate over the other.

“It is not up to the United States who will lead Afghanistan,” he said. “Nor should it be.”