KABUL (Reuters) - Former finance minister Ashraf Ghani was named Afghanistan’s president-elect on Sunday after he signed a deal to share power with his opponent, ending months of turmoil over a disputed election that destabilised the country as most foreign troops prepare to leave.

The announcement withheld the final election numbers, apparently as part of the political deal between Ghani and rival Abdullah Abdullah, a former foreign minister who claimed the process was rigged against him.

“The Independent Election Commission of Afghanistan declares Dr. Ashraf Ghani Ahmadzai as the president of Afghanistan,” commission chief Ahmad Yousuf Nuristani said.

Under the terms of the unity deal, Ghani will share power with a chief executive proposed by Abdullah. The two will share control over who leads key institutions such as the Afghan army and other executive decisions.

Nuristani acknowledged deep flaws in the June 14 run-off vote and said that a U.N.-supervised audit was not adequate to weed out all the vote-rigging. The audit was organised at a cost of $10 million to be paid for by U.S. aid money, according to the United Nations.

The final sticking point in the negotiations to form a unity government was Abdullah’s insistence that the official final vote tally not be released, his aides have said. Abdullah was widely believed to be far behind in the official results.

He appeared to have won a concession to at least delay making the results public, though Nuristani said on Sunday the full count would be provided at a later date.

Ghani and Abdullah ratified the power-sharing agreement earlier on Sunday at the presidential palace, joined by outgoing leader Hamid Karzai. The rivals-turned-partners shared a brief embrace after signing the papers.

Ghani is expected to be sworn in as president on Sept. 29, according to a senior Afghan official.

Afghan rival presidential candidates Abdullah Abdullah (L) and Ashraf Ghani exchange signed agreements for the country's unity government in Kabul September 21, 2014. REUTERS/Omar Sobhani

The negotiated end to the crisis was far from the smooth election process that the U.S. and its allies had envisioned. They had hoped for an exemplary democratic transfer of power ahead of the end of the military mission that started with the 2001 ouster of the Islamist Taliban for sheltering al Qaeda leaders behind the 9/11 attacks on the United States.

Still, after weeks of election uncertainty that at times seemed about to descend into political and ethnic violence, the resolution was greeted with relief by many Afghans.

“The six-month election deadlock damaged life for Afghans,” Kabul resident Mohammad Alim said. “We didn’t have normal sleep, investors fled from Afghanistan, people were worried about their future ... but today people are relaxed and happy.”

Besides the difficulties of forging an effective government after so much acrimony, Afghanistan’s new administration faces huge challenges in fighting an emboldened Taliban-led insurgency that is launching near-daily attacks across the country.

It will also face significant difficulty in improving the lives of Afghans who face hard times as tax revenues plummet, aid flows fall and contracts with the NATO-led coalition dry up with most foreign troops leaving by the end of the year.

SECURITY AGREEMENT

The accord signed on Sunday was the finalisation of a broader power-sharing structure brokered by U.S. Secretary of State John Kerry, who swiftly welcomed its signing.

Speaking in New York before the start of U.N. meetings, Kerry said the naming of a new Afghan president was an opportunity to sign a bilateral security agreement with the United States.

“They have joined together in a unity government that offers a huge opportunity for progress in Afghanistan, for the signing of the BSA in a week or so, inauguration next week for the new president, and importantly for a real program of unity and reform to be implement on behalf of the people of Afghanistan,” Kerry said before a meeting with his French counterpart.

One of Ghani’s first acts would be to sign the long-delayed security agreement as he has previously declared support for the pact to allow a small force of foreign troops to remain in Afghanistan after 2014.

There is a risk that any instability could be exploited by neighbours, like Pakistan, whose past involvement in Afghan affairs have played a part in the conflicts that have dogged Afghanistan for decades.

“A difficult and challenged unity structure is still preferable to conflict between these two groups,” said a U.S. official in Kabul.

“Having them both working together within the government and direct their energies toward positive reform is again preferable to some of the alternatives.”

Ghani, an ethnic Pashtun, and Abdullah, whose main support comes from the country’s second largest ethnic group, the Tajiks, face a difficult task forging unity in a country riven by ethnic and tribal rivalries.

Abdullah’s accusations that the run-off election was rigged in Ghani’s favour had raised fears of ethnic violence, which could have ignited a broader conflict.

“A spark could have dealt a strong blow to the political process, if today’s deal had not happened,” commented Waliullah Rahmani, director of the Kabul Center for Strategic Studies.

“Afghanistan will now be able to move forward for the next five years, he said, “Even though it is not an ideal government.”