(Adds Wardak voting, U.S. Secretary Kerry statement)
By Praveen Menon and Mirwais Harooni
KABUL, June 14 (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.
Violence spiked on the day as militants launched hundreds of attacks with rockets, explosives and gunfire, leaving at least 20 civilians dead, along with a further 11 police and 15 army personnel, the interior ministry said.
But clashes did not deter millions of voters from turning out and feared high-profile attacks did not materialise. Instead, voting ended at 4 p.m. (1130 GMT) with a palpable sense of relief, at least 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.
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.
Further afield, the picture looked less rosy, however, with voters in provinces such as Wardak refusing to come out to vote.
"Maybe it's because in the second round people have lost belief in the election," said Masuma, a school teacher in Wardak. "The reason is fraud, they think that their vote is useless." [ID:nL5N0OV0L8}
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.
U.S. Secretary of State John Kerry acknowledged the concerns by saying that now the voting was finished, the work of the Afghan electoral authorities was paramount.
"It is essential that the process of tallying votes, adjudicating complaints, and finalising the results be transparent and accountable and that the candidates and other stakeholders work with the electoral commissions and respect their conclusions," he said in a statement.
Both candidates have complained repeatedly that electoral organisers are incompetent and biased.
Late on Saturday, Abdullah again raised the issue, questioning the turnout figures provided by the electoral commission, saying he doubted they had been as high as reported.
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.
"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.
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 Pravin Char and Sonya Hepinstall)