By Praveen Menon and Mirwais Harooni
KABUL (Reuters) - Millions of Afghans turned out for a second time on Saturday to elect a successor to President Hamid Karzai on Saturday, a decisive test of the country's ambitions to transfer power democratically for the first time in its tumultuous history.
Most foreign troops will leave by the end of 2014, and whoever takes over from Karzai will inherit a troubled country plagued by an assertive Taliban insurgency and an economy crippled by corruption and the weak rule of law.
The run-off pitted former anti-Taliban fighter Abdullah Abdullah against ex-World Bank economist Ashraf Ghani after neither secured the 50 percent majority needed to win outright in the first round on April 5.
While violence spiked on the day with at least 20 civilians reported dead and a further 16 security force members killed in clashes with militants, the interior ministry said, the high-profile attacks that had been feared did not materialise.
Voting ended at 4 p.m. (1130 GMT) with a palpable sense of relief in the Afghan capital.
"I'm from this country so I am never afraid of threats," said Lajiullah Azizi, a hospital worker who voted in western Kabul just minutes after a small bomb exploded at his polling station. "I hope this election will bring peace."
Officials immediately began counting ballots, although Afghanistan's difficult terrain, where ballot boxes have to hauled by donkey from some of its remotest corners, means preliminary results will not be known until July 2.
Karzai, standing down after 12 years in power marked by increasingly sour relations with the West, is certain to retain a hand in politics but has been tight-lipped about his plans.
"Today Afghanistan takes a step towards stability, development and peace. Come out and determine your destiny," Karzai, clad in his trademark green Afghan robe, said after casting his ballot.
Karzai's relationship with the West has deteriorated sharply over his refusal to sign a security pact with the United States allowing a small contingent of U.S. forces to remain in the country beyond 2014. Both Abdullah and Ghani have promised to sign it promptly.
"Looking ahead, the United States stands ready to work with the next president of Afghanistan," the U.S. ambassador to Afghanistan, James B. Cunningham, said in a statement.
"Today marks the start of a new era for Afghanistan, and Afghans can be proud of what they have achieved."
Twelve million voters were eligible to cast ballots at 6,365 polling centres across Afghanistan, from windswept deserts on the Iranian border to the rugged Hindu Kush mountains.
Despite the outbreaks of violence, voters defied threats and long queues snaked out of polling centres in urban areas soon after voting began at 7 a.m.
Turnout was more than seven million, election commission chief Ahmad Yousuf Nuristani said, roughly the same as in the first round of voting in April.
The turnout was so high that some 333 voting centres ran out of ballot papers, sparking minor protests by disgruntled voters. The election commission said additional materials were later distributed and calm was restored.
"Security is a concern but the people of Afghanistan have defied security threats so far," Abdullah said.
The election has been fraught with accusations of fraud by both candidates and many fear a close outcome will make it less likely the loser will accept defeat, possibly dragging Afghanistan into a risky, protracted stand-off over the vote.
"We ask everyone to prevent and discourage people from fraud and vote-rigging so that we can have a transparent, free and fair election," Ghani said after casting his vote in west Kabul.
Officials are concerned by the prospect of a close outcome that could, in the worst-case scenario, propel the country back into war along ethnic lines.
Both candidates set the stage for complaints, repeatedly accusing electoral organisers of incompetence and bias.
The United Nations has urged candidates not to attack the organisers, to safeguard the process.
"There's a short-term gain only in trying to undermine or bully the institutions at the expense of their legitimacy," said United Nations deputy chief in Afghanistan Nicholas Haysom.
"It's going to be the legitimacy of the elections which will give legitimacy to the new head."
Abdullah polled 14 percentage points ahead of Ghani in the first round with 45 percent of the vote, but Ghani, who is ethnic Pashtun, stands to gain a portion of the Pashtun vote that was splintered in the first round.
Pashtuns are Afghanistan's biggest ethnic group, making up about 45 percent of the population. While Abdullah is partly Pashtun, he is identified more with the ethnic Tajik minority.
The chance of an equal split between candidates is hard to gauge because there are few reliable polls. ACSOR research centre, asking respondents to choose between Abdullah and Ghani, predicted a 50:50 split before the first round.
(Writing by Maria Golovnina; Additional reporting by Hamid Shalizi and Jessica Donati; Editing by Rosalind Russell and Sonya Hepinstall)