MOSUL, Iraq — Despite clinging to only a sliver of territory in Mosul, Islamic State militants managed to launch a counterattack Friday that reversed days of Iraqi army territorial gains in just a matter of hours – a setback that underscores the fragility of the Iraqi security forces despite years of U.S.-led coalition training as well as the instability likely to follow the city’s liberation.

The offensive began just after noon, when 50 to 100 IS fighters began firing on units of the Iraqi army’s 16th Division charged with holding the northwest frontline in the Mosul’s Old City neighborhood.

The attack broke through the army’s first line of defense and the rest of its lines soon crumbled.

The surprise attack illustrated the resilience of the extremists who, though controlling less than a square half-mile of territory, have maintained the ability to conduct both conventional military counterattacks and insurgent strikes.

Hassan, a 45-year-old soldier with the 16th Division, described the close-fought battle inside the rubble-strewn alleyways of the Old City.

“Daesh started to attack us from everywhere. We were so close to them that we even fought with hand grenades,” he said referring to the Islamic State by its Arabic acronym.

“We have lots of martyrs and wounded soldiers, but we can’t evacuate them. It was epic,” Hassan said, giving only his first name in line with military regulations.

The initial wave of Iraqi army casualties began arriving within an hour at a field hospital a few hundred yards from the front, carried on stretchers by medics on foot through the Old City’s perilous terrain.

The neighborhood’s narrow roads, once passable on motorcycles, are now covered with rubble and downed power-lines, and the footpaths that lead in and out of the Old City wind through houses, across rooftops, beside airstrike craters and down into basements.

At least five soldiers were killed and 25 wounded, said a doctor at the field hospital.

Meanwhile, south of Mosul, IS has successfully retaken a pocket of territory declared liberated months ago.

“The attack started two days ago. Daesh took Imam Gharbi village,” said Salah Hassan Hamid, the mayor of Qaryara, a nearby town. He said policemen and tribesmen allied with the Iraqi military were sent in, but clashes were still ongoing and only half the village had been brought back under government control.

Two Iraqi journalists were killed and IS took a number of local residents hostage, the mayor added.

Following that attack, the U.N.’s migration agency suspended operations at two nearby camps – the Qayara air strip emergency site and the Haj Ali camp – where nearly 80,000 displaced Iraqis live.

The fighting prevented six water-tanker trucks from entering the Haj Ali camp, where temperatures reached 122 degrees in recent days, agency spokesman Joel Millman said.

Despite the setbacks, coalition spokesman U.S. Army Col. Ryan Dillon said the counterattacks were costing IS large numbers of fighters and not having an impact on the overall operation to defeat the militant group.