Taliban insurgents, their ranks swelled by foreign fighters pushed across the border from Pakistan, nearly surrounded this northern Afghan city last month with an offensive that stunned local authorities and raised concerns over their ability to defend the country without U.S. and foreign combat troops.

Under pressure from a yearlong military offensive in neighboring Pakistan, the Taliban and allied militants -- some waving the black flags of the Islamic State group -- appear to be trying to carve out a new safe haven in northern Afghanistan that could give them access to Central Asia and China, Afghan officials say.

As Afghanistan's U.S.-trained and equipped forces have struggled to fend off the insurgents -- who at one point came within 3 kilometers (less than 2 miles) of Kunduz -- authorities have increasingly turned to local militias and former warlords, a further indictment of the costly, decade-long U.S. effort to build an effective Afghan military.

Provincial Gov. Mohammad Omer Safi said 3,000 troops are now battling a well-armed insurgent force of some 2,000 fighters who crashed against the city's gates late last month at the start of the spring fighting season. He said logistical problems that left troops without food, fuel or ammunition for days on end have been resolved, but that the Taliban have proven tough to dislodge.

"We have surrounded the enemy everywhere and we will not allow them to advance any further," he told The Associated Press. "Maybe with the passage of time they will be weakened, run out of ammunition and find themselves moved back. This is not a one day or two days, or one month or two months fight."

Safi said that in addition to switching from hit-and-run attacks to seizing and holding territory, the Taliban have also been joined by other militants, including from the Islamic Movement of Uzbekistan. The IMU has longstanding ties to the Taliban and al-Qaida, and is believed to have pledged allegiance to the IS group last year.

There are ominous signs of a growing IS presence in northern Afghanistan. Safi said fighters have raised the group's black flag in nearby villages, and that foreign fighters from Turkey, Chechnya, Uzbekistan, Tajikistan and Kyrgyzstan have been found among the dead.

A Western intelligence official said the extent of the IS presence in Afghanistan remains unclear, saying "there is a lot of self-branding, rather than franchising."

But an Afghan intelligence official said the group is present in at least four of the country's 34 provinces, mainly in the south. He pointed to the beheading of seven Afghan soldiers in northern Badakhshan province last month, noting that the tactic has rarely been used by the Taliban. Both officials spoke on condition of anonymity as they were not authorized to comment publicly on the matter.

Gen. John F. Campbell, the commander of international forces in Afghanistan, said Saturday that the IS group is actively recruiting but is not yet operational in the country. He also noted reports that IS militants have clashed with the Taliban.

Safi, the Kunduz governor, said the army had managed to push back the insurgents to Gor Tepa, some 15 kilometers from the provincial capital, but that their progress was slow because the Taliban were occupying civilian homes and using human shields.

"We cannot destroy the houses of the poor civilians when the Taliban are inside so we cannot use our heavy artillery against the enemy," he said.

In the city itself life has largely returned to normal, but the scorched remains of the fighting are visible on its outskirts, where officials and residents say the Taliban torched homes and other buildings as they retreated. U.N. agencies say 18,000 families, or around 100,000 people, were displaced by the fighting.

In the village of Talawka, around 8 kilometers from the city, Commander Assadullah led 30 men armed with their own assault rifles, rocket-propelled grenades and machine guns on a recent patrol past mud-brick compounds, many of them reduced to rubble and occupied only by ducks and donkeys.

The men arrived here on May 13 from a nearby district, and are among 1,000 militiamen called up by provincial authorities and given monthly stipends. Assadullah, 50, who fought the Soviets in the 1980s, said Afghanistan's security forces are no match for the Taliban's "professional fighters."

The insurgents "have strong front lines, they are using anti-personnel and anti-tank mines. They use the houses and the basements for cover," he said. He added that his men would be unable to drive them out without better air and ground support from the military.

Safi said the insurgents are determined to carve out a new safe haven after being driven out of Pakistan's North Waziristan tribal region by a military offensive launched nearly a year ago.

The Kunduz province, a relatively wealthy region of grain and cotton fields, sits astride the old Silk Road and would provide access to neighboring Central Asian countries as well as China, all of which have grappled with Islamic militancy. Safi said the Taliban are already forcing farmers to hand over 10 percent of their crops to the group as a tithe.

"They are fighting very hard to have a safe place in northern Afghanistan," Safi said.