KABUL (Reuters) -- The rival candidates in Afghanistan's messy election for a new president finally struck a power-sharing deal Saturday, aides said, after more than two months of tension over a vote in which each side accused the other of fraud.

The crisis has further destabilised Afghanistan just as the U.S.-led military coalition prepares to withdraw most foreign troops at the end in the year, leaving Afghan forces to fight the Taliban insurgency.

Teams from both sides had met late into the night with United Nations representatives to try to finalize a power-sharing deal before Sunday's scheduled announcement of the final election results after a U.N.-monitored audit and recount.

The last sticking point was how to announce the results of the audit. Aides and officials have said candidate Abdullah Abdullah, a former foreign minister, had insisted that the official percentages either not be made public at all or be altered to give him more votes. Abdullah is widely believed to be trailing his rival, ex-Finance Minister Ashraf Ghani, in the final tally.

It was unclear late Saturday how the dispute over results was eventually resolved, but all sides in the talks said the deal was done.

"Both camps have agreed 100 percent on everything and we'll sign the deal tomorrow. Everything has been initialled and there is no disagreement on anything," saidFaizullah Zaki, a spokesman for Ghani.

Mujib Rahimi, spokesman for Abdullah, also confirmed a deal had finally been struck, but did not give any details.

Aimal Faizi, a spokesman for President Hamid Karzai, said the deal would be formally signed on Sunday in a ceremony with Karzai.

"They have agreed on the announcement (of results) as well," Faizi said in a text message.

Preliminary figures released in July put Ghani, a longtime World Bank official, ahead with 56 percent of the vote, prompting street protests from supporters of Abdullah, who alleged massive fraud and said he was the rightful winner.

The dispute ruined hopes for a smooth democratic transition to replace Karzai, who has held power since the Taliban's hardline Islamist government was ousted in 2001 with U.S. military support, and threatened to rekindle the ethnic tensions that had plunged Afghanistan into civil war in the 1990s.

Abdullah derives much of his support from the ethnic Tajik and Hazara communities, while Ghani is widely supported by the Pashtuns, the largest ethnic group.

Final election results have been delayed for weeks to await an audit of all 8 million ballots cast in the June 14 run-off vote between Ghani and Abdullah, the top finishers in the first round in April.

The audit was part of a deal brokered in July by U.S. Secretary of State John Kerry to try to avert a descent into violence.

Both Abdullah and Ghani pledged to accept the audit results and to form a unity government, with the winner - presumably Ghani - as president and the runner-up holding the position of or nominating a chief executive with expanded powers. Wrangling over how much power that chief executive will wield has been a key sticking point in the power-sharing deal.

A draft of the agreement seen by Reuters would give the chief executive the power to decide on a near-equal number of key government positions. It also calls for the unity government to call a loya jirga - a traditional Afghan council of elders - to help reform election laws and prevent future crises.

How well the rivals could share power and work together after the bitter election feud is uncertain. Both Ghani and Abdullah are pro-Western technocrats who ran on largely similar platforms of promoting education and poverty reduction, as well as ending the war with the Taliban.

The drawn-out uncertainty over Afghanistan's next leader has emboldened the Talibaninsurgency to fill the vacuum and launch yet more attacks across the country.

One of the new president's first tasks is likely to be signing a bilateral security agreement with the United States that would allow a force of a few thousand foreign soldiers to remain after 2014 ends to train Afghan security forces.

Karzai has refused to sign the document, which is linked to continuation of aid needed to pay Afghan civil servants, teachers and soldiers.

Afghanistan's finance ministry has warned it will soon run out of money without foreign aid.