The two contenders for Afghanistan's presidency, Ashraf Ghani and Abdullah Abdullah agree to share power in a deal signed Sunday at the presidential palace in Kabul. (Reuters)

The two contenders for Afghanistan's presidency, Ashraf Ghani and Abdullah Abdullah agree to share power in a deal signed Sunday at the presidential palace in Kabul. (Reuters)

Former finance minister Ashraf Ghani was declared the winner of Afghanistan’s contested presidential election Sunday, setting the stage for President Hamid Karzai’s departure from office and a security agreement allowing American troops to remain in the country after this year.

Ghani, 65, will become Afghanistan’s second president since the fall of the Taliban in 2001 and will face the continued threat of Islamist militancy, a severe budget shortfall that threatens to bankrupt the government, and rampant public corruption.

But before he was declared the winner in Afghanistan’s grueling year-long election process, Ghani agreed Sunday to share power with the second-place finisher, Abdullah Abdullah. The deal, which had been a top priority of the Obama administration, calls for Ghani to name Abdullah, 54, the country’s chief executive as soon as the inauguration is held.

The new government is cause for fresh optimism among U.S. leaders who had grown weary of Karzai. The outgoing president had refused to sign a long-term security agreement that will allow up to 15,000 foreign troops to remain in the country after the official NATO mission ends this year.

Ghani and Abdullah, a former foreign minister, have pledged to sign the agreement immediately upon taking office this week or early next week. That would allow President Obama to stick with the plan he outlined in May calling for 9,800 American troops to remain in Afghanistan next year.

1 of 10 Full Screen Autoplay Close Skip Ad × Ghani is declared the winner in Afghanistan View Photos Ashraf Ghani will become Afghanistan’s second president since 2001, and he has agreed to share power with the second-place finisher, Abdullah Abdullah. Caption Ashraf Ghani will become Afghanistan’s second president since 2001. Supporters of Ashraf Ghani celebrate in a street in Kabul, the Afghan capital, after he was named president-elect. Omar Sobhani/Reuters Buy Photo Wait 1 second to continue.

There is already considerable skepticism, however, about whether men who had been bitter rivals can jointly lead a country that has a long history of ethnic tension and violence.

“The national unity government is like a painkiller drug,” said Jafar Mahdawi, a lawmaker from Kabul who supported Ghani.

“In the short run [it] will mitigate the tensions, but in the long run will be the source of troubles.”

The White House took a more hopeful stance in a statement issued Sunday. “This agreement marks an important opportunity for unity and increased stability in Afghanistan,” it said. “We continue to call on all Afghans — including political, religious, and civil society leaders — to support this agreement and to come together in calling for cooperation and calm.”

Afghans first flocked to the polls in April to choose among eight candidates who were vying to complete the country’s first democratic transition of power from one elected government to another.

But as the process extended into a June runoff between Ghani and Abdullah, and then a recount to try to resolve allegations of widespread fraud, Afghans have at times appeared disenchanted with democracy.

On Sunday, despite naming Ghani the winner, the Afghan Independent Election Commission did not release his margin of victory, even though the election and recount had cost the international community $147 million. Instead, commission chief Yousef Nuristani simply said Ghani had prevailed while conceding that hundreds of thousands of votes cast for both candidates were suspected of being fraudulent.

“The audit was inclusive and credible, but it couldn’t uncover all items of the fraud,” Nuristani said on national television.

On Sunday evening, however, the Ghani campaign took it upon itself to release final results that it said the commission gave to both campaigns earlier in the day.

The figures show that Ghani emerged from the recount with about 55 percent of the vote after about 850,000 votes were discarded during the audit.

Also earlier Sunday, Ghani and Abdullah joined Karzai in the presidential palace to sign the power-sharing agreement. The rivals embraced but did not speak, leaving it to Karzai to address an election-fatigued nation.

“The Afghan people have been waiting for this happy day,” said Karzai, who has said he plans to remain in Afghanistan after he steps down in the coming days. “I hope the things I couldn’t do, you two can do.”

Under the terms of the agreement, Ghani and Abdullah will split appointment-making powers. Both will sit on the country’s National Security Council, but Ghani will retain ultimate control over the country’s armed forces.

The agreement also stipulates that the chief executive will answer to the president. Abdullah, however, will “function as prime minister” and be responsible for “implementing government policies,” the agreement states.

As the Obama administration faced additional international crises this summer in Iraq and Ukraine, the enactment of the deal became an urgent priority for Secretary of State John F. Kerry.

Kerry brokered the agreement this summer when Abdullah protested the results of the June runoff that showed Ghani prevailing by more than 1 million votes. That initial deal appeared near collapse in recent weeks, prompting stern calls from Kerry and Obama to the two candidates.

In a statement Sunday, Kerry hailed the agreement as a historic step. “These two men have put the people of Afghanistan first, and they’ve ensured that the first peaceful democratic transition in the history of their country begins with national unity,” he said.

But doubts remain about whether Abdullah and Ghani will be able to work together in a unity government.

Mohammad Amin Farhang, who served as minister of both commerce and economy during Karzai’s first term, said Ghani was at times “bad-tempered and pushy” while Abdullah had a reputation for being “stubborn.”

Farhang said he fears it is only a matter of time before a major split develops between them or their high-profile supporters, many of whom are expected to gain positions in the new government.

“The two gentlemen have the ability to move the country in the right direction, if they learn from their past mistakes. But if they haven’t learned from past mistakes, they will take the country into a crisis like Iraq,” Farhang said in a recent interview. “And if they start to clash with each other, the army will split into two parts.”

Ghani is an ethnic Pashtun who draws the bulk of his support from southern and eastern Afghanistan. Abdullah’s mother was Tajik, but he had a Pashtun father and considers his power base to be in ethnically diverse northern Afghanistan.

The key to the new government’s success will be a clear delineation of power between the presidency and the new position of chief executive, said Abdul Hameed Mubariz, a former deputy information minister and political analyst.

“This is not about two men, it’s about a great country,” Mubariz said recently.

“The people voted for only one of them and did not vote to divide the country and divide the power.”

Even senior administration officials said they foresee challenging days ahead for the new government.

“Of course there will be disagreement, and it will be hard work that it stays on track,” one official said.

On Sunday, as news of the agreement spread through Kabul, Afghans appeared divided on the new government’s prospects of success.

For months, Afghans have blamed this year’s steep economic decline on the lack of a new president. Many said they are optimistic that the incoming government will boost investor confidence and improve the job market.

“There has been no business since the election-related tensions started,” said Mohammad Faisal, a 31-year-old shopkeeper in Kabul. “If they work together and put aside their differences, everything will be all right.”

For other Afghans, the agreement stokes fears about the effectiveness of government.

“I see no good future for the national unity government,” said Yousef, a 28-year-old taxi driver who like many Afghans uses only one name.

“If it takes this long for them to reach an agreement on a national government, God knows in the future how much contention will take place in the appointment of a single police chief or a district governor.”

Ghani, a former World Bank official, spent part of his life in the United States but returned to Afghanistan after the fall of the Taliban to serve in Karzai’s government in the early 2000s. He left his job as finance minister in 2004 and became head of Kabul University.

Abdullah, an eye doctor, was a top aide to legendary Afghan guerrilla commander Ahmed Shah Massoud during Afghanistan’s civil war in the 1990s. He later became a diplomat and served as foreign minister from 2002 until Karzai ousted him in 2006.

During this year’s campaign, Abdullah and Ghani had emphasized a desire to improve relations with the United States.

Obama plans to withdraw all U.S. forces from Afghanistan by the end of 2016, but the United States is expected to continue to spend $5 billion to $8 billion per year in Afghanistan for at least a decade.

The United States was also one of 14 countries responsible for covering the $147 million price tag of the Afghan election and recount, according to the United Nations.

Khalid Pashtoon, a lawmaker and close Abdullah adviser, predicted that support and continued investment from the international community, which accounts for about 65 percent of Afghanistan’s annual budget, is one reason he expects Ghani and Abdullah to succeed.

“This country is ruled by the system, not the people, and the system is already here,” Pashtoon said. “If there is any kind of lack of attention, they are the ones that will lose, not the system.”

Sharif Hassan contributed to this report.