KABUL - The rival candidates in Afghanistan’s messy election for a new president finally struck a power-sharing deal on 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 US-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 finalise a power-sharing deal before Sunday’s scheduled announcement of the final election results after a UN-monitored audit and recount.

“The IEC will officially announce the final result of the presidential election tomorrow,” Independent Election Commission spokesman Noor Mohammad Noor told AFP. The election process has been plagued by delays and setbacks, and the latest timetable for results could still change unless the remaining disputes are ironed out.

“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,” said Faizullah Zaki, spokesman for front-running candidate and former finance minister Ashraf Ghani. Aimal Faizi, a spokesman for President Hamid Karzai, said the deal would be formally signed on Sunday in a ceremony with Karzai.

But Mujib Rahman Rahimi, a spokesman for Abdullah, said the two candidates had not yet signed a power-sharing deal. “Most of the differences have been solved. The only one which remains is over how the election results are announced,” he said. “The IEC is not supposed to announce the final results before the agreement is finalised. I hope they won’t do that.”

Preliminary results released in July put Ghani 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.

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 US Secretary of State John Kerry to try to avert a descent into violence.

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 US military support, and threatened to rekindle the ethnic tensions that had plunged Afghanistan into civil war in the 1990s.

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

Ghani – who won the vote according to preliminary results – is set to emerge as president, with Abdullah nominating who will fill the new post of “chief executive officer”, possibly taking on the role himself. Under the Afghan constitution the president wields almost total control, and the new government structure faces a major test in the coming years as the country’s security and economic outlook worsens.

President Hamid Karzai, whose successor was originally due to be inaugurated on August 2, was constitutionally barred from standing for a third term in office. He has stayed publicly neutral in the election. The United Nations has expressed fears that any backlash to the election result could revive the violence of the 1990s, when nationwide chaos allowed the Taliban to come to power.

About 41,000 NATO troops remain in Afghanistan, down from a peak of 150,000 in 2010, fighting along Afghan soldiers and police against the fierce Taliban insurgency. NATO’s combat mission will end in December, with a follow-on force of about 12,000 troops likely to stay into 2015 on training and support duties.

After the June election was engulfed in fraud allegations, the US brokered a deal in which the two candidates agreed to abide by the outcome of an audit of all eight million ballot papers and then form a national unity government. But Abdullah later abandoned the audit, saying it was failing to clean out fraud.

Only ten days ago, he insisted he had won fairly and that negotiations over the unity government had collapsed. Street protests by either side’s loyalists risk spilling into serious unrest because Abdullah draws his support from Tajiks and other northern ethnic groups, while Ghani is backed by Pashtun tribes of the south and east.