Updated

U.S. Secretary of State John Kerry made a hastily arranged visit to Afghanistan on Friday to help resolve an election crisis sowing chaos in a country that the U.S. has spent hundreds of billions of dollars and lost more than 2,000 lives trying to stabilize.

The visit comes as Afghanistan shows worrying signs of unraveling in its first democratic transfer of power from President Hamid Karzai, who followed a decade of Taliban governance.

Kerry was to meet Friday with the two candidates claiming victory in last month's presidential election runoff.

"I've been in touch with both candidates several times as well as President Karzai," Kerry said before leaving Beijing, where he attended a U.S.-China economic meeting. He called on them to "show critical statesmanship and leadership at a time when Afghanistan obviously needs it."

"This is a critical moment for the transition, which is essential to future governance of the country and the capacity of the (U.S. and its allies) to be able to continue to be supportive and be able to carry out the mission which so many have sacrificed so much to achieve."

With Iraq wracked by insurgency, Afghanistan's power dispute over the election results is posing a new challenge to President Obama's 5 1/2-year effort to leave behind two secure nations while ending America's long wars in the Muslim world.

Obama wants to pull out all but about 10,000 U.S. troops from Afghanistan by the end of the year, and the election of a new Afghan president was supposed to enshrine the progress the nation has made since the U.S.-led invasion after the 9/11 terrorist attacks.

The preliminary results of the presidential election runoff suggested a massive turnaround in favor of former Finance Minister Ashraf Ghani Ahmadzai, a onetime World Bank economist who lagged significantly behind former Foreign Minister Abdullah Abdullah in first-round voting.

Abdullah, a top leader of the Northern Alliance that battled the Taliban before the American-led invasion, claims the runoff was a fraud, and his supporters have spoken of establishing a "parallel government," raising the specter of the Afghan state collapsing. Abdullah was runner-up to Karzai in a fraud-riddled 2009 presidential vote before he pulled out of that runoff.

Chief electoral officer Zia ul-Haq Amarkhail has resigned, denying any involvement in fraud but saying he would step down for the national interest.

Kerry will seek to persuade both candidates to hold off from rash action while the ballots are examined and political leaders are consulted across Afghanistan's ethnic spectrum. The U.S. wants to ensure that whoever wins will create a government that welcomes all ethnic factions.

If neither candidate gains credibility as the rightful leader, the winner could be the Taliban. Many Afghans fear the insurgent forces will only gain strength as the U.S. military presence recedes. Internal instability could aid the insurgency.

Abdullah and Ghani each have said that as president they'd sign a bilateral security agreement with the United States, granting American forces immunity from local prosecution. Without such an agreement, the Obama administration has said it would have to pull all U.S. troops out of Afghanistan, a scenario that played out in Iraq three years ago. Karzai has refused to finalize the deal, leaving it to his successor.

James Dobbins, the State Department's special representative for Afghanistan and Pakistan, said this week some degree of fraud was expected, but it's believed the fraud was "quite extensive."

Speaking in Washington, Dobbins said the Abdullah campaign particularly mistrusts the impartiality of the Afghan electoral institutions.

Both campaigns and Karzai have asked the U.N. for help, he noted, and the U.N. has been designing a plan for deciding how ballots can be reviewed and which ones would be reviewed for possible fraud.

A U.N. audit, however rudimentary, probably could be done within two weeks, U.S. officials believe. The focus would be on clear fraud indicators, including districts with high turnout or more women going to the ballots than men.

Kerry also will meet with Karzai and U.N. officials.

Obama spoke to each candidate this week, asking them to allow time for investigations of ballot-stuffing. The White House said Tuesday that Obama warned that any move outside the law to seize power would mean the end of U.S. financial aid to Afghanistan.

Obama differentiated Afghanistan from Iraq, which he declared a "dumb war," while considering Afghanistan a fight worth waging, ordering tens of thousands of new troops into the country in his first year in office.

The risk of a prolonged Afghan political crisis has alarmed U.S. officials already struggling to respond to sectarian tensions in Iraq that have broken out into open warfare.

The situations in Afghanistan and Iraq are distinct. But in each, the U.S. has spent more than a decade trying to set up democratic governments that could effectively police their own territories and stamp out threats to the American homeland.

And in both countries that objective is in peril, their futures threatened by a combination of poor leadership, weak institutions, interethnic rivalry and fierce extremist rebellions.

Suicide bombers and gunmen staged a deadly assault on government compounds Wednesday in southern Afghanistan, killing 30. The U.N. warned this week that such fighting in populated areas was a major cause for a 17 percent uptick in civilian deaths this year in a report that cast doubt on the capacity of government soldiers and police to protect the Afghan people after most U.S. and foreign forces leave.