The 13-year NATO combat mission in Afghanistan formally ended Sunday with a ceremonial retirement of its green flag and a pledge by top officials of the U.S.-led coalition to remain reliable partners in Afghanistan’s unfinished war against the Taliban and other militant groups.

Scores of Afghan and foreign officials gathered to witness the symbolic shift to a new, much smaller NATO assistance and training mission. The event was held in a basketball gym inside NATO headquarters here in the Afghan capital and accompanied by a brass band and a color guard.

“Our commitment to Afghanistan endures. . . . We are not walking away,” promised Gen. John F. Campbell, the U.S. commander of the outgoing International Security Assistance Force mission. He will lead the new NATO support mission, which technically begins at midnight Dec. 31.

Campbell and other Western officials stressed that their chief function under the new mission, named Resolute Support, will be to advise, train and assist Afghan security forces. They said, however, that a separate “non-NATO” contingent of U.S. forces will participate in force protection, logistical support and counterterrorism activities.

The Taliban responded to the transition event with glee. In a lengthy statement issued Sunday night by a Taliban spokesman, the insurgent group gloated at the final departure of a “haughty” superpower that “thought it had already won the war and that the Mujaheddin would never . . . think of putting up a fight.”

Security is tight on the streets of Kabul ahead of a ceremony to mark the end of NATO combat missions in Afghanistan. (Reuters)

The statement said the NATO withdrawal was proof that “the infidel powers who thought they would turn Afghanistan into their strategic colony” had been “pushed to the brink of defeat.”

The total number of international troops here, which peaked in 2009 at about 142,000, has gradually shrunk to about 17,000. Under Resolute Support, officials said, 12,500 to 13,500 NATO forces will remain in 2015, including about 5,000 American troops. Twenty-eight NATO allies and 14 partner nations will contribute in different ways, the alliance said. Officials said about 5,500 U.S. forces will be part of the second contingent, which will be based in Kabul.

Western and Afghan officials at the event described the shift in upbeat terms. They praised the dedication and bravery of Afghan security forces, now numbering about 350,000, and predicted that the Afghans will continue to wage a strong fight on their own against Taliban and al-Qaeda insurgents.

In a written statement Sunday, President Obama lauded the “responsible conclusion” of the Afghanistan war, even though he acknowledged that the country remains a “dangerous place.” He also thanked “every American — military and civilian” for their service and said that the United States is more secure because of their sacrifices.

Gen. Hans-Lothar Domröse, a senior NATO official based in Brussels, told the Kabul audience that Afghan forces have shown the “ability, will and confidence to defeat the enemy.” He said opinion polls show that 88 percent of Afghans have confidence in the national army and 72 percent in the national police. “Today begins a new chapter for NATO as an enduring partner of the Afghan government,” he said.

But the withdrawal of international combat support comes at an especially tense time for Afghanistan, with the Taliban aggressively testing the will of the new national unity government. Since early November, Taliban forces have waged an unprecedented terrorism campaign in the capital and made steady inroads in several provinces, such as Helmand, where U.S. and British forces once held sway.

In addition, the deadly Dec. 16 siege of a military-run school in northwest Pakistan by Taliban militants has unleashed a flurry of actions by Afghan, Pakistani and foreign forces, including U.S. drone strikes, in the volatile border area where both Afghan and Pakistani Taliban insurgents are active.

Early this month, the Obama administration said it will leave up to 1,000 more troops than originally planned in Afghanistan beyond year’s end. A change in the long-delayed Bilateral Security Agreement, signed by Afghan officials in September and ratified by parliament in November, allows U.S. troops to engage in counterterrorism operations against the Taliban and other insurgents.

Another problem is that three months after the installation of the power-sharing government, composed of President Ashraf Ghani and top electoral rival Abdullah Abdullah, a cabinet has yet to be formed, leaving a worrisome leadership void in the Defense and Interior ministries as well as in the police intelligence department.

At the transition ceremony Sunday, the only Afghan official to speak was the civilian national security adviser, Mohammad Hanif Atmar. He expressed gratitude for the sacrifices made by NATO forces. About 3,500 international troops have been killed and tens of thousands wounded since 2001.

“We recognize that you carried on the fight for us when we were not ready,” he said. “We pray for the fallen, for your sons and daughters who died on our soil.”

Atmar insisted that Afghan forces are now fully ready to defend their country, but he stressed that they cannot do it without foreign assistance. “We don’t want or expect your support to be indefinite, but we need it now more than ever,” he said.

In interviews Sunday, several Afghan security officials expressed similar concerns, saying their ground forces are motivated but poorly equipped and heavily reliant on foreign troops for air support — especially bombing raids — in tough encounters with insurgents.

This year has accounted for a record number of casualties among Afghan forces, totaling more than 5,000 military and police personnel. Desertion rates continue to be high, and Afghan officials say reducing casualties will be crucial to maintaining current force levels.

In its statement Sunday, the Taliban claimed that its fighters had “pushed back” foreign and Afghan forces from strategic districts of Helmand province in the south, including areas that were once under coalition control.

Over the past several months, the insurgent group said it had “captured and liberated” large parts of Musa Kala, had taken 40 military posts in Greshk and was reaching its fortified center, and had regained key parts of Marja that had been secured by coalition forces in a major campaign.

A senior Afghan military commander in Helmand, Gen. Syed Malik, said his men “will continue to fight bravely” after foreign forces are gone, “but the enemy knows we don’t have the air force or helicopters, or enough artillery and heavy weapons. We need those to lower our casualty rate,” he said. “Once we have those, I assure you we will defend Afghanistan very well.”