KUNDUZ, Afghanistan — Huddled in brightly lit yards late one recent night, hundreds of inmates taunted a team of about a dozen special forces who were rounding the walls along the top of Kunduz prison.

The appearance of elite soldiers was an anomaly, a sign to the prisoners that something was happening. “What’s going on?” they shouted. “Is tonight going to be our last night in here?

Taliban fighters planned to storm the compound that evening, according to information gathered by local intelligence officers. Government forces hoped the show of force would spur prisoners — some in possession of smuggled cellphones used to communicate with the Taliban — to wave off the attack.

Without enough fighters to hold the city’s front lines and reinforce the prison, the special forces’ move was a gamble. But it appeared to work: The night passed without incident.

As Taliban militants close in on Afghanistan’s provincial capitals, they are inching closer to central prisons that house around 5,000 of their fellow fighters, leaving the government scrambling to secure the detention facilities. If just a fraction of the detainees were to escape, Afghan security officials warn, it would hand the militants a significant advantage on the battlefield, where they are already making steady gains.

Taliban leaders are telling their fighters “it’s extremely important that we release these people because they are experts, and we need them to strengthen our forces,” said a local security official in Kunduz briefed on the matter. Like other officials in this story, he spoke on the condition of anonymity because he was not authorized to speak to the media.

Securing the release of Taliban fighters is a powerful rallying cry with wide appeal among the movement’s rank-and-file. As the group is rapidly expanding its territorial control, Afghan government officials say the militants need more foot soldiers and commanders to hold their gains.

Afghanistan’s Interior Ministry and Office of Prison Administration declined to release specific numbers of Taliban fighters in government custody, but the local official in Kunduz and a senior Afghan security official in Kabul said the number is around 5,000.

The effort to guard those inmates intensified after dozens of prisoners escaped Badghis central prison when the Taliban breached that province’s capital city in early July. An investigation into the attack found the prison break had been facilitated by prison employees paid off by the Taliban, according to the provincial governor, Hasamuddin Shams.

“That prison was the primary objective of the Taliban attack,” Shams said, explaining that the Taliban fighters in his area needed recruits and news coverage to keep their rank-and-file motivated. “We were not expecting such a high level of betrayal.”

The incident at Badghis prison — similar to reports of deals made between the Taliban and government forces that have allowed the group to take districts with little to no resistance — prompted a review of prison management nationwide, the security official in Kabul said. Prison administrators in nearly a dozen provinces were fired for corruption charges and mismanagement as a result of the review, he said.

“This is a big problem,” said the senior security official in Kabul, who added that significant work remained to root out corruption. “Hundreds of millions of dollars were wasted,” he said, primarily referring to foreign investment in the detention facilities.

The official said that after the Badghis escape, prison guards in vulnerable cities were sent reinforcements, including more advanced riot gear and weaponry. In Kunduz, a provincial capital in northern Afghanistan that is surrounded by Taliban-controlled districts, a cluster of abandoned civilian homes around the prison have been transformed into an outer layer of security.

Similar steps were taken in Kandahar, another key city that is under siege by the Taliban in the country’s south. The perimeter around Kandahar’s prison is one of the most heavily reinforced in the city, with multiple checkpoints established by Afghan commando, army and police special forces units.

“The Taliban have tried to break this prison many times,” said a police special forces officer stationed near the compound. A few weeks ago, a team of foreign Taliban fighters moved into a base near the prison in an effort to launch a complex attack involving an armored tank suicide bomb, he said.

Other prisoners considered “high value” — between 200 and 300 from several different provinces — were simply moved to Kabul’s central prison, the security official in Kabul said. More than 100 such detainees were transferred out of Kandahar after the Taliban pushed within a few hundred meters of the city’s prison in July. Dozens of others were moved from Ghazni, Badakhshan and Takhar.

The Taliban denied targeting prisons.

“Sometimes if there is fighting close to the cities, the prisoners also riot and the guards flee,” said Zabiullah Mujahid, a Taliban spokesman. “That allows the prisoners to escape.”

But prisoner releases continue to be a central demand of the group’s political leadership in Doha, Qatar, where peace talks have been stalled for months.

“We want to release our prisoners,” Mujahid said, “but not [through force] because it would put their lives at risk.”

The Afghan government released more than 5,000 Taliban members last year ahead of peace talks but now claims the intended gesture of goodwill only strengthened the militants’ hand when many returned to the battlefield, according to statements from Afghanistan’s national security council.

“I know for a fact they went straight back to fighting,” said the security official in Kunduz. The official listed the names of Taliban fighters he arrested who were among the thousands released, claiming he personally recaptured around 30.

Taliban political leadership has accused the Afghan government of reimprisoning “many” of the Taliban members released in the lead-up to talks but said claims the men had returned to the battlefield were “baseless.”

Suhail Shaheen, a spokesman for the group’s political office in Doha, said the prisoners rearrested were living peacefully and many were arrested from their homes during night raids. He said such actions are “spoiling the opportunity for peace” in Afghanistan.

“The Taliban will always break their commitment,” the local security official in Kunduz said, calling for a recently detained alleged Taliban member to be brought into his office.

The man was gaunt and filthy. The official’s secretary handed him a plate of melon that he ate hurriedly. The official asked him if he would return to fighting if he was released, and the man pledged not to. The official laughed.

“I don’t expect change,” he said. “They are like animals from the jungle.”

Aziz Tassal in Kunduz and Ezzatullah Mehrdad in Kabul contributed to this report.