By Lesley Wroughton and Maria Golovnina
KABUL, July 13 (Reuters) - It was late on Saturday evening in a fourth-floor room of the U.S. ambassador's residence in Kabul that the election crisis in Afghanistan that had threatened to divide the nation was staved off.
U.S. Secretary of State John Kerry was in the room with former foreign minister Abdullah Abdullah, one of the two warring presidential candidates.
The door was opened to Abdullah's rival and bitter foe Ashraf Ghani.
Ending months of bitter squabbling, Ghani walked arms wide open toward Abdullah, embracing him warmly and shaking his hand, according to a U.S. official who was present.
A few hours later the two candidates announced they had agreed to a full recount of the disputed June 14 vote, meaning there would be a delay in the Aug 2 inauguration of a new president to replace Hamid Karzai, who was installed in office by the United States after the ouster of the hardline Islamic Taliban regime in 2002.
It took Kerry a marathon 44 hours of "intensive" and "emotional" talks to broker a deal between the two candidates, whose rupture had threatened to divide Afghanistan along ethnic lines, said several U.S. and Afghan officials who were involved in the process.
With the bulk of U.S. forces scheduled to withdraw from the war-torn nation this year, the row over the election and the possibility of violence has rekindled fears of a civil war and the prospect that the Taliban would take control again.
Preliminary results from the run-off vote in June 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.
Abdullah's support is mainly in the north, among the Tajik minority, while Ghani is supported by Pashtun tribes in the east and south. Tensions had soared earlier in the week when Abdullah's supporters threatened to form a parallel government after preliminary results from the vote were announced.
In a hurriedly arranged trip after visiting China, Kerry flew into Kabul late on Thursday night. He spoke with the U.S. ambassador to Afghanistan, James Cunningham, until 3:30 a.m. to map out a strategy on how to resolve the dispute, said U.S. officials who were aware of what transpired.
Four hours later, after a short nap, Kerry's first meeting was with U.N. special envoy to Afghanistan Jan Kubic, followed by separate talks with Ghani and Abdullah.
He was to meet with both candidates separately again and then finally bring them together after the ritual breaking of the Islamic Ramadan fast on Saturday evening, the officials said.
Kerry started his meetings with the two candidates one-on-one, said the U.S. officials, who spoke on condition of anonymity.
He already knew both men well, which allowed him to go directly into serious conversations that might have otherwise taken longer. A lot of "heavy lifting" was in the one-on-one sessions, according to the officials.
For weeks, the approach to ending the crisis had been two-track: a technical one in which talks focused on how to set up an audit of the votes that would restore the credibility of the election process; and a second, more difficult aspect - how to build an inclusive and broad-based government.
President Karzai had already been in talks with the two sides about the formation of a new government, but tensions over the election made the conversations difficult.
It was clear to the Americans that to resolve the crisis, questions over the recount, in particular the scope and implementation of it, had to be dealt with first to clear the way for a political dialogue.
Gradually the discussion moved to a full recount of the votes, which to the Americans was simpler but would also cost more in both time and money.
A complete audit would significantly improve the credibility of the outcome, officials said. Details of the implementation of the audit were left to U.N. experts, who drew on their expertise from a global array of recounts and audits.
The breakthrough moment in the talks came when the candidates began talking about doing a full audit of the votes, according to U.S. officials.
Discussions on how an audit could possibly look had already been studied by the United Nations. "We picked up where they left off and continued that dialogue," said an official.
Afghan sources say that Ghani agreed to a total recount because he is firmly convinced of his victory. Under the preliminary results, he is leading by more than a million votes.
On the other hand for Abdullah, it is a face saving solution. He has said he would accept defeat as long as the vote is completely clean, and the complete revision of the ballot boxes would give him a chance to exit gracefully, if he loses.
A source in Abdullah's camp said Abdullah managed to convince Kerry there was widespread fraud in the vote and that a recount was necessary.
"Kerry and Abdullah met twice and those meetings lasted for hours. The temperature was friendly but sometimes very serious. (Abdullah) expressed his concerns repeatedly during those meetings," the source said.
"(Kerry) accepted all our demands and promised to give us what was in our right. (Abdullah's camp) warned that if we don't compromise and reach a peaceful conclusion, Afghanistan will turns to its bad days again, America will withdraw all its forces and all their sacrifices and money they spent in the past 13 years will be wasted."
U.S. officials said there was nothing in the agreement that pre-judges the outcome of the audit or the election. Both candidates supported the approach, both of them agreed upfront to abide by the results of an audit.
Most of the progress was on Saturday, according to U.S. officials, with final agreement reached late in the evening.
When Abdullah and Ghani met in Kerry's presence, it was the first time they had talked to each other in months.
The men then sat down in armchairs on either side of Kerry, one U.S. official said. Ghani was dressed in traditional Pashtun clothes, a shalwar kameez or a loose tunic and trousers, and rolled prayer beads in his hands, which he says also helps his arthritis.
Abdullah was in a grey suit, seated opposite Ghani with Kerry in the middle.
After the final details were agreed, the three men held a joint news conference at the United Nations compound in Kabul just before midnight on Saturday.
All agreed that the best way out of the acrimonious and protracted deadlock was to delay the inauguration and recount all the ballots from scratch.
And in a show of unity after months of bitter bickering, Ghani kissed Abdullah on the cheek. Then they embraced. (Additional reporting by Mirwais Harooni; Editing by Raju Gopalakrishnan)