Kabul, Afghanistan (CNN) -- The United States and Afghanistan signed a long-delayed security agreement Tuesday that will allow U.S. troops to stay in Afghanistan beyond the previous December deadline to withdraw.

The deal was signed by Afghan National Security Adviser Mohammad Hanif Atmar and U.S. Ambassador to Afghanistan James Cunningham during a ceremony in Kabul.

The future of the U.S. military's presence in Afghanistan beyond 2014 had been in doubt. Former Afghan President Hamid Karzai negotiated the security deal but then reneged, and it became clear that no agreement would be signed until after presidential elections this year.

In February, the Obama administration announced for the first time that it had begun planning for the possible withdrawal of all U.S. troops by the end of 2014 if Afghanistan did not sign the agreement, which spells out the rights of U.S. troops operating there.

But both major Afghan presidential candidates pledged that they would sign the deal if elected this year. After an election in which results were delayed by accusations of voting fraud, former Finance Minister Ashraf Ghani signed a power-sharing deal with rival candidate Abdullah Abdullah, and Ghani was sworn in as president on Monday.

Obama hails 'historic day'

U.S. President Barack Obama welcomed the signing of the security agreement, which he said was the culmination of two years of hard work by both sides.

"Today, we mark an historic day in the U.S.-Afghan partnership that will help advance our shared interests and the long-term security of Afghanistan," he said in a statement.

Afghan and NATO officials also signed a deal Tuesday allowing allied forces to continue a NATO support mission after the end of this year, he said.

"These agreements follow an historic Afghan election in which the Afghan people exercised their right to vote and ushered in the first peaceful democratic transfer of power in their nation's history," Obama said.

"We look forward to working with this new government to cement an enduring partnership that strengthens Afghan sovereignty, stability, unity and prosperity and that contributes to our shared goal of defeating al Qaeda and its extremist affiliates."

U.S. Secretary of State John Kerry congratulated Ghani and Abdullah, as well as the Afghan people.

"Afghans have taken a moment of challenge and turned it into a moment of real opportunity," he said.

He also paid tribute to the contributions of Karzai, saying that while America's relationship with him has been "punctuated by disagreements," he is recognized by the world as "a nationalist, a patriot and an important figure who stepped forward when his country needed him."

Taliban: 'Death to America'

Even with the agreement, the United States is poised to wind down its military involvement in Afghanistan.

Obama has said that under the security agreement, virtually all U.S. forces would be out of Afghanistan by the end of 2016, shortly before his presidency ends.

He has called for 9,800 U.S. troops to stay there after the end of 2014, along with some allied forces. The number would get cut roughly in half by the end of 2015, and a year later, the U.S. military presence would scale down to what officials described as a "normal" embassy security contingent.

Meanwhile, the Taliban, who were ousted from power in Afghanistan by a U.S.-led military coalition in 2001, have continued to mount deadly attacks on high-profile Afghan targets and are fighting fiercely for control of important areas in the country.

Taliban spokesman Zabiullah Mujahid posted a succinct message on Twitter shortly after the security deal was signed Tuesday: "Death to America!"

CNN's Masoud Popalzai reported from Kabul. CNN's Jason Hanna reported and wrote from Atlanta. CNN's Kevin Liptak, Elise Labott and Laura Smith-Spark contributed to this report.