* Kerry mediated between feuding parties in Kabul
* Preliminary results put Ghani well ahead
* Dispute over vote undermines smooth transition of power (Recasts, adds more quotes)
By Lesley Wroughton and Maria Golovnina
KABUL, July 12 (Reuters) - U.S. State Secretary John Kerry convinced Afghanistan's feuding presidential candidates on Saturday to agree to a total recount of last month's presidential election, which has threatened to split the country along ethnic lines.
After two days of intense talks between Kerry, Abdullah Abdullah and Ashraf Ghani, all parties agreed that the best way out of the acrimonious and protracted deadlock was to delay the inauguration and recount all the ballots from scratch.
Preliminary results from the run-off vote on June 14 put Ghani, a former World Bank official, well ahead but Abdullah rejected the result, claiming widespread fraud and calling the outcome a "coup" against the Afghan people.
In a joint news conference with Kerry held just before midnight, the two rivals agreed to abide by the outcome of a U.N.-supervised recount.
"Both candidates have committed to participate in and abide by the results of the largest and most comprehensive audit," Kerry said. "Every single ballot that was cast will be audited... This is the strongest possible signal by both candidates of the desire to restore legitimacy to the process."
The recount was scheduled to begin within 24 hours, but was likely to take several weeks, meaning that a presidential inauguration scheduled for Aug. 2 will have to be postponed.
The dispute has raised concerns about a smooth transition of power in Afghanistan just as U.S.-led troops are leaving after 12 years of fighting the Taliban.
The stakes are high for the United States. Washington hopes to settle the dispute quickly so it can sign a security pact with Afghanistan allowing a contingent of U.S. troops to stay in the country beyond this year.
Unlike incumbent Hamid Karzai, both Abdullah and Ghani have both promised to sign the deal promptly, but the standoff over the vote has delayed the process.
After his own talks with Kerry, Karzai said he welcomed the initiative and hoped the full audit would start quickly:
"The people of Afghanistan have been patient and they are in a hurry and would like to hear as soon as possible the result of the election and witness their next president."
Abdullah's support is mainly in the north, among the Tajik minority, while Ghani is supported by Pashtun tribes in the east and south.
Observers have warned that if the dispute is not resolved quickly, Afghanistan could split along ethnic lines, with further violence likely.
Karzai, catapulted to the presidency 12 years ago by Western powers after the ousting of the Taliban, is expected to retain influence after stepping down, and Abdullah has accused him of rigging the vote in favour of Ghani.
Abdullah and Ghani had not met in person since the vote but Kerry managed to bring them together at a U.N. compound in central Kabul to give a joint news conference.
"This audit will be conducted in accordance with the highest international standards," said Kerry, flanked by both candidates. As he announced the total recount, many in the audience gasped.
"The auditing will be internationally supervised in a manner proposed by the U.N. assistance mission; the candidates' campaigns will each provide joint oversight of the audit."
In a show of unity after months of bitter bickering, Ghani kissed Abdullah on the cheek after addressing reporters.
"Since we have agreed to a 100 percent audit of ballots, I request from President Karzai to postpone inauguration of a new government," Abdullah said.
In comments to reporters on Friday, Kerry said Afghanistan's transition to a self-reliant state hung in the balance unless the legitimacy of the election could be restored.
Washington has warned of repercussions if either side declares victory and tries to grab power illegitimately.
The United States is in the process of withdrawing its forces from Afghanistan after more than a decade of fighting Taliban insurgents, but the country remains dependent on foreign aid, principally from Washington, its biggest foreign donor. (Writing by Maria Golovnina in Kabul; Editing by Kevin Liffey)