Afghan forces retook control of the strategic northern city of Kunduz on Thursday, officials said, but gunfights were ongoing after a three-day Taliban occupation that dealt a stinging blow to the country's NATO-trained military.
The fall of the provincial capital, even temporarily, highlights the stubborn insurgency's potential to expand beyond its rural strongholds in the south of the country.
Afghan forces, hindered by the slow arrival of reinforcements but backed by NATO special forces and US air support, struggled to regain control of the city after three days of heavy fighting.
But on Thursday troops managed to reach the centre of Kunduz after an overnight counteroffensive. Residents told AFP that the streets were littered with Taliban bodies and that gun battles are still echoing in parts of the city.
"(Afghan) special forces now control Kunduz City, it is retaken and being cleared (of) terrorists," interior ministry spokesman Sediq Sediqqi said on Twitter, adding that the insurgents had suffered heavy casualties.
Deputy Interior Minister Ayoub Salangi said the city had been recaptured after a "special operation" overnight.
However, Taliban spokesman Zabihullah Mujahid insisted: "This morning we have driven out Afghan forces from the city and the Taliban are still in control."
But an Afghan Taliban commander who spoke to AFP from an undisclosed location said that Taliban fighters were retreating from Kunduz. "The Taliban have almost vacated the main parts of the city but let me make it clear that we have proved that we can take control of any other city whenever we want," he said.
Some scenes of jubilation erupted at dawn around the city square where local residents, who suffered three days of crippling food shortages, thanked government troops.
"Afghan soldiers took down the white-and-black Taliban flag in the city square and hoisted the government flag," Kunduz resident Abdul Rahman told AFP.
"The Taliban suffered heavy casualties last night. Dead bodies are scattered on the streets, and their supporters are carrying them out of the city wrapped in white cloths."
Security officials said the militants had slowly infiltrated Kunduz during the recent Eid festival, launching a Trojan Horse attack that enabled them to capture it within hours on Monday.
The development coincided with the first anniversary of Ashraf Ghani's national unity government.
Marauding insurgents seized government buildings and freed hundreds of prisoners, raising their flag throughout Kunduz.
The lightning capture of the city sent thousands of panicked residents fleeing as insurgents erected checkpoints across the city and were seen racing vehicles stolen from the police, UN and Red Cross.
Militants, showing off seized tanks and armoured cars, had issued edicts against looting and vowed to enforce Islamic sharia law.
Rights group said the insurgents exposed civilians to grave danger by hiding in people's houses and conducting door-to-door searches for Afghan soldiers and government staff.
The Taliban's recent gains in Kunduz and neighbouring provinces highlight that a large and strategic patch of northern Afghanistan is imperilled by a rapidly expanding insurgency.
It is also seen as a game-changer for the fractious militant movement that has been dogged by a leadership crisis since the announcement in July of founder Mullah Omar's death.
"The Taliban know that they don't have the power to retain control of a big city like Kunduz," Kabul-based military analyst Atiqullah Amarkhil told AFP.
"But their takeover, however temporary, shows they are a force to reckon with before any future peace negotiations."
Their incursion into Kunduz, barely nine months after the NATO combat mission concluded, raised troubling questions about the capabilities of Afghan forces as they battle the militants largely on their own.
It has also renewed questions about Washington's plan to withdraw most US troops from Afghanistan next year.
Even after years of training and equipment purchases -- on which Washington spent $65 billion -- Afghan forces have been unable to rein in the ascendant insurgency.
The Taliban stepped up attacks during a summer offensive launched in late April against the Western-backed government in Kabul.
After years of costly involvement, most NATO troops pulled back from front lines by the end of 2014, although a residual force of around 13,000 remains for training and counter-terrorism operations.