John Kerry and Afghan presidential candidates Abdullah Abdullah (centre) and Ashraf Ghani smile as they speak at a press conference in Kabul on Saturday announcing an audit of the votes cast in the country's general election.

AFP

He was only on the ground for 48 hours, but America's top diplomat John Kerry may well have helped prevent Afghanistan from sliding into another bloody ethnic war.

The helicopters were waiting for the US Secretary of State when he arrived in Kabul from Beijing early Friday on a last-minute mission to broker an end to a tense political impasse.

Some 48 hours later they flew him back over the darkened city streets from the fortress-like US embassy compound to his plane - a deal triumphantly in his hand.

Few had believed it possible that Kerry could bring together two presidential rivals bitterly at odds over disputed elections, and help avert fears of ethnic violence in a country ravaged by decades of war.

But the indefatigable 70-year-old dropped a political bombshell when he announced that rivals Abdullah Abdullah and Ashraf Ghani had agreed that every one of the eight million votes cast in their June run-off poll would be audited.

Just days earlier the two men were barely speaking, as Abdullah vehemently accused Ghani of stealing victory by stuffing the ballots.

But standing next to each other with Kerry late on Saturday before the world's cameras, they clasped hands, smiling, and raised them in the air.

With that single dramatic show of unity, Kerry may well have helped guide Afghanistan's young democracy away from a return to the bloody ethnic civil war of the 1990s.

The announcement followed two days of marathon meetings, with Kerry and his staff shuttling between the two candidates and their teams in the embassy.

He also met for hours one-on-one with each candidate.

"It wasn't until the 11th hour... that we had a full agreement," a senior US official who took part in the negotiations told journalists.

The talks had focused on the best mechanism for auditing the poll results.

A second track sought to persuade the feuding rivals to meet to discuss their differences and clear the way towards a national unity government.

Afghan analyst Younus Fakour said the agreement had "prevented a disaster in Afghanistan, it prevented the creation of a parallel government, it prevented chaos" - and the national unity government element was crucial.

"Before, the candidates were afraid that if one wins, the other will be left out. But with this they are sure they will be part of the next government," he told AFP.

By midday Saturday it became clear that given doubts over a UN proposal to audit votes from around 35% of polling stations, it made more sense to work towards an unprecedented review of all ballots cast.

As reporters awaiting a press conference at UN mission headquarters broke the Ramadan fast with a hastily arranged Iftar of take-away lamb, rice, bread and dates, Kerry persuaded the two men to meet face-to-face.

Ghani, a former World Bank economist, was asked to join talks with Abdullah as they also hammered out a structure for a national unity government.

"When Dr Ghani walked in, the two approached each other and immediately embraced and there was clearly a degree of affection between the two," the first US official said.

The US will now support calls from the head of the UN Assistance Mission in Afghanistan, Jan Kubis, for additional resources and manpower to help audit all ballots in coming weeks.

"You deliver miracles," Kubis told Kerry, as they got the final seal of approval from outgoing President Hamid Karzai.

Kate Clark of the Afghan Analysts' Network hailed the US role and said the deal's "strong international underpinning" gave cause for optimism.

While it still remains unclear how the agreement will play out, one Afghan official emailed a member of Kerry's team voicing heartfelt thanks and adding that "most Afghans will sleep more peacefully tonight".