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.
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.
The creation of the government of national unity will now diminish the importance of the election result. I understand that after the audit the final gap between the two candidates was less than three points – a significant cut from Ashraf Ghani’s lead after the first count of 13 points.
A president who has built an international reputation as an incorruptible technocrat is tarnished by the fact that most of the fraud in the election was discovered to be in his votes.
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.
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.
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.
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
Are you in Afghanistan? What is your reaction to the national unity deal? You can email your comments to firstname.lastname@example.org.
Or send us your comments using the form below.
If you are happy to be contacted by a BBC journalist please leave a telephone number that we can contact you on. In some cases a selection of your comments will be published, displaying your name as you provide it and location, unless you state otherwise. Your contact details will never be published. When sending us pictures, video or eyewitness accounts at no time should you endanger yourself or others, take any unnecessary risks or infringe any laws. Please ensure you have read the terms and conditions.
Terms and conditions