Image copyright Reuters Image caption Abdullah Abdullah, left, and Ashraf Ghani signed the agreement in the presidential palace in Kabul

A deal to form a government of national unity in Afghanistan has been signed at a ceremony in Kabul.

The signing comes after months of wrangling following presidential elections in April and June.

Under the deal, Ashraf Ghani becomes president while runner-up Abdullah Abdullah nominates a CEO with powers similar to those of prime minister.

Later, the election commission declared Mr Ghani as winner of the bitterly fought poll.

"The Independent Election Commission declares Ashraf Ghani as the president, and thus announces the end of election process," commission chief Ahmad Yousaf Nuristani said without declaring the number of votes for each candidate or the turnout figures.

Both sides had accused the other of fraud following the election and the months of uncertainty have damaged the economy and worsened insecurity.

Mr Ghani and Mr Abdullah signed the agreement at a televised ceremony inside the presidential palace in the capital, Kabul. They then stood and embraced each other.

Image copyright AP Image caption Afghans watched the historic ceremony on live television

Outgoing Afghan President Hamid Karzai congratulated the two men, saying that the agreement was "for the progress and development of this country".

"On behalf of the Afghan nation, I am congratulating them on this understanding and agreement," he said in his speech.

The US hailed the deal as an "important opportunity for unity".

"We support this agreement and stand ready to work with the next administration to ensure its success," White House spokesman Josh Earnest said.

Nato, which operates the international peacekeeping force in Afghanistan - said it hoped both leaders could move forward "in the spirit of genuine political partnership".

The power-sharing deal was finally reached after a comprehensive audit of all eight million votes, brokered by US Secretary of State John Kerry.

BBC Afghanistan correspondent David Loyn, who has seen a copy of the agreement, says it averts potential violence by supporters of Mr Abdullah.

The agreement says the new CEO will be answerable to Mr Ghani, although he has lost a battle to be sworn in after the announcement of the election result, our correspondent says.

Analysis: David Loyn, BBC News, Afghanistan

The new Afghan government will have a cabinet of ministers, including the CEO and two deputies, chaired by the president who will take strategic decisions. Day-to-day administration will be carried out by a new Council of Ministers, chaired by the CEO, and including all ministers.

One major issue that divided both camps was over appointments. Abdullah Abdullah won the fight to be able to appoint senior positions on terms of "parity" with Ashraf Ghani, and "the two teams will be equally represented at the leadership level".

But appointments further down will be "equitably" shared - so there will not be a one-for-one handout of jobs across the country. Ashraf Ghani is impatient to make major reforms, and has secured the wording he wants on the formation of a "merit-based" mechanism to appoint senior officials.

An official announcement of how many votes each candidate won is still awaited - one source told me the margin of victory could be as close as 3% but other figures being quoted by Afghan officials say it's more like 10%. Nothing is certain unless or until Afghanistan's Independent Election Commission publishes the final result.

What the Afghan power-sharing deal means

Profile: Ashraf Ghani

Profile: Abdullah Abdullah

The new chief executive - nominated by Mr Abdullah - will be side-by-side with the president when he is inaugurated in the next few days.

Mr Abdullah will be able to appoint senior positions on terms of "parity" with Mr Ghani. The agreement says "the two teams will be equally represented at the leadership level".

However, our correspondent says there will not be a one-for-one handout of jobs further down and that could lead to arguments.

The agreement calls for a spirit of partnership. But after a bitter election campaign and months of wrangling, the stability of this government cannot be guaranteed, he adds.

Image copyright Reuters Image caption US Secretary of State John Kerry helped to negotiate an audit of the election result

Earlier, Mr Abdullah told the BBC that he accepted Mr Ghani should be leader.

A spokesman for Mr Ghani said that there was no longer any dispute between the two sides.

One of the new president's first tasks is widely expected to be signing a bilateral security agreement with the US.

The deal will allow a small force of soldiers to remain beyond 2014 to train Afghan security forces.

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

Mr Ghani and Mr Abdullah have both promised to sign it.

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

Update 22 September 2014: This article has been amended to make clear that the official vote tallies for each candidate have not yet been confirmed.