HERAT/KABUL — Taliban insurgents on Wednesday stormed the capital of Afghanistan’s northwestern Badghis province, officials said, briefly taking over police headquarters and sparking panic among local people.

Officials said airstrikes were being carried out and special forces had been deployed to push the Taliban fighters back.

The Islamist insurgents have been advancing across the country in recent weeks, even as the United States pulled out of its main base, effectively bringing its two-decade intervention to an end. Taliban gains have been especially dramatic in northern provinces where the fighters had long been kept at bay.

Provincial governor Husamuddin Shams told Reuters the Taliban had attacked the city of Qala-e-Naw from three directions in the morning and Afghan security forces were fighting them back.

“They entered some parts of the city, but later on the enemy was faced with a strong reaction,” he said.

A ministry of defence official said on condition of anonymity that the insurgents took control of the provincial office of the National Directorate of Security (NDS) and police headquarters, and were attempting to take over the governor’s office before special forces pushed them back.

“They were inside the city and the city briefly collapsed,” said the official.

Fawad Aman, Deputy Spokesman for the Ministry of Defence said government forces had managed to take back control of the police and NDS offices and within the next few hours they expected to clear the city of Taliban fighters.

Abdul Aziz Bek, head of Badghis’ provincial council said that Afghan forces began air strikes against Taliban fighters during the afternoon, after earlier describing a state of panic in the city.

“Qala-e-Naw was in a state of disarray as security forces and people do not know what to do now,” he said. “More than 200 prisoners in the central prison of the Badghis broke the prison gate and escaped.”

The United States, which toppled the Taliban for sheltering Osama bin Laden in 2001 and has since propped up the Kabul government, agreed under then-President Donald Trump last year to pull out subject to Taliban security guarantees.

His successor, Joe Biden, rejected advice from his generals to hold on longer so that the government could make a political deal with the Taliban. American troops pulled out of the main base at Bagram airfield north of Kabul on Friday, days after their commander warned that Afghanistan might descend into full-blown civil war without them.

The prospect of a Taliban victory is alarming for millions of urban Afghans, particularly women and girls who were banned from school or most work under their rule. The Taliban say they have changed, but many remain skeptical.

As foreign forces withdraw, Taliban fighters have been swiftly gaining ground across towns in the north and western provinces, forcing soldiers to surrender and civilians to flee.

Talks between Afghan government and Taliban negotiators in Qatar have failed to make substantive progress in recent months, though the warring sides have been holding meetings in recent days.