Image copyright Reuters Image caption Mr Ghani (left) and Mr Abdullah agreed to a UN-supervised recount of votes

A deal to form a united government in Afghanistan is "an Afghan solution to an Afghan problem", US Secretary of State John Kerry says.

Presidential rivals Ashraf Ghani and Abdullah Abdullah have been feuding over the result of the June elections.

"We will form a government of national unity... What unites us is far greater than what divided us during the campaign," Mr Ghani said on Friday.

Mr Kerry held talks earlier with both men in Kabul.

It is not yet clear what the government structure will be under the power-sharing deal.

Mr Ghani stressed that the deal signed on Friday was a "commitment to co-operation" and Mr Abdullah said they had both "accepted the need to work together".

The US would like a new Afghan government team to be ready in time for a Nato summit in early September.

Most foreign troops will pull out at the end of this year, though Taliban insurgents have stepped up their attacks in recent months.

Image copyright Reuters Image caption Mr Kerry (right) said the vote audit must achieve "a credible result"

How rival candidates compare Ashraf Ghani Abdullah Abdullah Technocrat and former World Bank official. Open to talks with Taliban Former anti-Soviet resistance member. Wary of Taliban talks Leading in Pashtun-dominated southern provinces Ahead in mainly Tajik northern areas Backed by Rashid Dostum, an Uzbek ex-warlord accused of human rights abuses Supported by wealthy Balkh governor Atta Mohammad, a bitter Dostum rival Has support of Qayyum Karzai, brother of President Karzai Also has backing of Mohamed Mohaqiq, powerful leader of ethnic Hazaras Ahmed Zia Masood, whose brother was a famous resistance hero, helped balance ticket Gul Agha Sherzai, an influential Pashtun, helped bring ethnic balance to ticket

Who actually won?

The presidential rivals agreed to a vote audit during Mr Kerry's last visit and both contenders said they would abide by the result.

All 8m ballots are being reviewed in an attempt to resolve the dispute over the result of the 14 June second round.

Preliminary results announced by Afghan election officials gave Mr Ghani 56.44% of the votes, with Mr Abdullah gaining 43.45%. But they accuse each other of electoral fraud.

The results were markedly different from those achieved in the first round of voting, held in April, when Mr Ghani came a distant second.

Mr Kerry met President Hamid Karzai and then Mr Abdullah, a former foreign minister, and Mr Ghani, who used to be finance minister.

Mr Kerry later spoke at a news conference with the two presidential rivals. Mr Kerry said "one of these men is going to be president, but both are going to be critical to the future of Afghanistan no matter what".

Mr Abdullah called the agreement "another step forward in the interests of strengthening national unity in the country, strengthening rule of law in the country and bringing hope to the people for the future of Afghanistan."

Mr Kerry flew in just two days after an Afghan soldier killed a US general in an "insider attack" at a UK-run military training facility.

Gen Harold Greene was the most senior US soldier killed in action overseas since the Vietnam War.