The United States and Afghanistan sign a security deal that allows some American troops to remain in Afghanistan beyond 2014, ensuring a continuing U.S. presence in the region. (Reuters)

The United States and Afghanistan on Tuesday signed a vital, long-delayed security deal that will allow nearly 10,000 American troops to remain in Afghanistan beyond the final withdrawal of U.S. and international combat forces this year.

The Bilateral Security Agreement (BSA), and a separate pact signed with NATO, permit the continued training and advising of Afghan security forces, as well as counterterrorism operations against remnants of al-Qaeda. The signing of the documents comes as Taliban insurgents are increasing their attacks in an effort to regain control in anticipation of the combat troops’ departure.

The accord was signed a day after Ashraf Ghani was sworn in as Afghanistan’s new president in a power-sharing government, marking the first democratic hand­over of power in the nation’s history. Ghani’s predecessor, Hamid Karzai, who had presided over the country since shortly after the Taliban was driven from power in 2001, had refused to sign the agreement, souring relations with Washington.

President Obama called it a “historic day” and said in a statement that the signing came after “nearly two years of hard work by negotiating teams on both sides.” Critics, particularly Republican lawmakers, have charged that the failure to negotiate a similar agreement in Iraq that would have allowed U.S. troops to stay in that country contributed to the rise of the threat from Islamic State militants there.

Although Obama had considered a complete withdrawal from Afghanistan, he announced in May that up to 9,800 U.S. troops would remain, provided that the agreement was signed, until the end of 2015. The accord allows U.S. basing at nine separate locations across Afghanistan.

The agreement allows for 9,800 U.S. troops to remain in Afghanistan past 2014, with the expectation that troop levels would be cut in half by 2016.

The troop number is to be cut in half by 2016, with American forces thereafter based only in Kabul and at Bagram air base. By the end of 2017, the U.S. force is to be further reduced in size to what U.S. officials have called a “normal” military advisory component at the U.S. Embassy in Kabul, most likely numbering several hundred.

The agreement also prevents U.S. military personnel from being prosecuted under Afghan laws for any crimes they may commit; instead, the United States has jurisdiction over any criminal proceedings or disciplinary action involving its troops inside the country. The provision does not apply to civilian contractors.

The NATO agreement, closely coordinated with the U.S. document, ensures that troops from countries including Germany and Italy will also stay, although in much smaller numbers.

State Department officials in Washington did not specify the scope and nature of the ongoing counterterrorism operations, which are to be conducted in partnership with Afghan forces. A number of al-Qaeda operatives have been ensconced for some time in the rugged mountains of eastern Afghanistan near the Pakistani border.

The officials, speaking on the condition of anonymity under conditions imposed by the administration, declined to comment on the future of separate U.S. drone strikes, conducted by the CIA from bases in Afghanistan, that continue to target al-Qaeda fighters and other militants in western Pakistan.

U.S. funding for Afghanistan — up to $8 billion annually for military and other assistance for at least the next three years — was heavily dependent on approval of the BSA.

U.S. and international combat forces, now numbering about 40,000, have been gradually withdrawing as the December deadline approaches; international forces turned over complete security responsibility to Afghanistan in June 2013. The State Department officials said that the extent to which the United States will contribute “enablers,” including logistical support, to the Afghan military was still under discussion but that any agreement would come under Obama’s 9,800 cap.

Tuesday’s signing took place at the presidential palace compound in central Kabul. The BSA was signed by U.S. Ambassador James B. Cunningham and Afghan national security adviser Mohammad Hanif Atmar inside a hall with a glittering chandelier and portraits of Afghanistan’s leaders over several centuries.

In the audience were Ghani and his former political rival Abdullah Abdullah, who is now the chief executive of the unity government. After months of political tensions over a disputed vote, both men agreed to share power after a compromise deal was brokered by the United States, staving off the threat of ethnic rifts and violence.

In remarks after the signing, Ghani assured dignitaries, as well as Afghans watching the nationally televised ceremony, that the BSA would not violate the country’s sovereignty and laws. He also assured Afghanistan’s neighbors that the extended presence of U.S. troops would pose no threat to them.

“I assure the nation that these agreements are in our national interest,” Ghani said. “If needed, these agreements can change with the consideration of the interests of both sides.”

A Taliban spokesman, Zabiullah Mujahid, declared the security pact an attempt by the United States to assert its authority over Afghanistan and its people.

But tribal leaders in areas targeted by the Taliban said Tuesday that they welcomed the accord and that they hoped it would deliver a blow to the resurgent militants, as well as to what they described as meddling by neighboring Pakistan and Iran.

“If the United States is honest with the security agreement with Afghanistan, the security agreement means prosperity for the Afghans and a heavy slap on the faces of Taliban, Pakistan and Iran,” said Haji Matu Khan, a tribal elder of the Marja district in volatile Helmand province.

On the streets of Kabul, Afghans expressed mixed feelings about a continuing U.S. troop presence.

“Signing the BSA is in the interest of Afghanistan. It will prevent civil war,” said Abdul Wahid Frogh, 28, a government employee. “From an economic perspective, the BSA will help Afghanistan attract aid and financial cooperation. As well, the BSA assures Afghan people that the international community will not let us go forward on our own, and this assurance will lead to an economic boom.”

But others were concerned about an ongoing presence and said Afghanistan had no choice but to sign the pact.

“If we don’t sign the BSA, our forces won’t be paid. If our forces are not paid, they don’t fight the Taliban. If they don’t fight, the Taliban will be back,” said Mohammad Sediq, 41, a schoolteacher. “But the presence of foreign troops under the BSA shouldn’t be for a long time, because Afghans can’t tolerate foreign forces here for a long time.”

DeYoung reported from Washington. Sharif Hassan contributed to this report.