KABUL, Afghanistan — The U.S. and its allies are growing increasingly concerned as Afghanistan shows signs of unraveling in its first democratic transfer of power from President Hamid Karzai. With Iraq wracked by insurgency, Afghanistan’s dispute over election results poses a new challenge to President Obama’s effort to leave behind two secure states while ending America’s long wars.

Secretary of State John Kerry made a hastily arranged visit to Afghanistan on Friday to help resolve the Afghan election crisis, which is sowing chaos in a country the U.S. has tried to stabilize, spending hundreds of billions of dollars and losing more than 2,000 U.S. troops. He planned to meet with the two candidates claiming victory in last month’s presidential election runoff.

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 Sept. 11, 2001, 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 U.S.-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 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 across Afghanistan’s ethnic spectrum are consulted. 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 U.S. 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.”

Both campaigns and Karzai have asked the United Nations for help, Dobbins 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.