Residents of western Mosul walk away from a food distribution site. The densely populated area is now the front line of fighting between Iraqi government forces and the Islamic State. (Aris Messinis/AFP/Getty Images)

Iraqi forces are suffering fierce counterattacks in areas of Mosul recaptured from the Islamic State, soldiers say, and are barely holding the main government compound that they triumphantly declared cleared earlier this week.

Police units have made a rapid push into the city over the past two weeks, reaching its main government buildings Tuesday. But forces were soon ambushed there in what one police officer described as a “well-planned trap,” while their grip on other neighborhoods they claim to control seemed tenuous during a recent trip to the city.

In the western neighborhood of Dindan, about half a mile from the government compound, forces with Iraq’s emergency response division, an elite unit of the police, came under frequent mortar fire two days later. Soldiers ducked behind walls to avoid snipers.

Capt. Waleed Ibrahim said Iraqi forces had lost control of two streets that morning after the militants deployed car bombs in a counterattack. Helicopters flew low overhead, strafing Islamic State positions.

The fight to retake Mosul is the biggest operation Iraq’s security forces have launched since the 2003 U.S.-led invasion. In the five months since the battle began, the militants have fought bitterly against tens of thousands of Iraqi troops backed by airstrikes and advisers from the U.S.-led coalition.

After eastern neighborhoods were secured weeks ago, the ferocious tug-of-war now is taking place in the city’s densely packed neighborhoods west of the Tigris River, which are home to about 750,000 people. Some 50,000 people have fled since the offensive for the western half of the city began two weeks ago.

Soldiers said rivalries among different parts of the Iraqi security forces had pressured them to overreach and put troops and civilians at risk.

In the raid on the main government compound, forces from the emergency response division took over the key administrative buildings with very little resistance early Tuesday morning, soldiers said.

But after the area was handed over to other police forces later that morning, the militants counterattacked.

“We took it for half a day, and then we were attacked very hard,” said Capt. Safaa al-Yasiri, who was setting up a position on the edge of the compound when the counterattack began. The militants used bulldozers to push down blast walls as a “very large number of fighters” attacked, he said.

“It was a very well-planned trap,” he said. “It was complete chaos.”

Police forces had not properly cleared the surrounding buildings, and the Islamic State positioned snipers on their roofs. Yasiri said his unit fled in their Humvees, but others were trapped inside the government compound as police forces sent reinforcements to break the siege on them.

“We don’t control the compound at the moment,” he said, adding that the interior minister arrived Wednesday night expecting to tour the area, only to be told that it wasn’t properly secured. “He was angry and left,” Yasiri said.

First Lt. Ashraf Hussein of the emergency response division, who was also there during the ambush, said the compound was now effectively a no man’s land. “It looks like they were expecting this attack and didn’t want to give it up easily,” he said.

A general from Iraq’s counterterrorism forces, speaking on the condition of anonymity because of the sensitivity of the topic, said police forces that led the push toward the main government compound are treating the conflict “like a competition.”

The counterterrorism troops led much of the fighting in eastern Mosul and have steadily progressed through their sector of the west.

“The only thing they care about is how many areas they take each day,” he said. “They want to prove that they are better than us at the expense of civilians.”

Families in the city are moving from neighborhoods on police front lines to others led by counterterrorism forces.

“Their method was shelling each neighborhood with artillery and rockets consistently and then attacking with Humvees,” the general said. “They are acting with recklessness and madness.”

Col. Abdulrahman al-Khazali, a spokesman for the federal police, said his forces are moving quickly because of the experience they have in fighting the Islamic State. Their specially engineered weaponry such as rocket launchers and artillery has also helped, he said.

“We don’t shell randomly,” he added.

In Dindan, residents said four neighboring houses that were reduced to rubble had been hit by artillery fire and seven civilians had died. An airstrike five days earlier destroyed a nearby home. The bodies of the inhabitants — a doctor, his wife and three children — were still under the rubble, according to Abu Mohammed, 45, who used a nickname for security reasons because the area had not yet fully been retaken.

“It’s a battle among residential neighborhoods, so of course there will be civilian casualties,” he said, adding that many more bodies had not been recovered from destroyed houses deeper inside the area.

Speaking at a forum in the Kurdish city of Sulaymaniyah this week, the United Nations humanitarian coordinator, Lise Grande, praised Iraqi security forces for attempting to uphold international humanitarian law and “putting civilian protection first.”

That had averted a humanitarian disaster in eastern Mosul, she said. Fewer people than aid agencies had expected fled during the offensive there.

But neighborhoods in the west are much more densely packed, and the rate of displacement has sped up, with as many as 10,000 people fleeing each day.

The airstrike-monitoring group Airwars said hundreds of civilians were killed in strikes in Mosul during the first week of March. The Iraqi Observatory for Human Rights said at least 700 civilians have been killed in fighting for the city’s western side, though numbers are hard to track in Islamic State-held neighborhoods. The militants also regularly fire artillery and rockets onto civilian neighborhoods, and medics said they had received 12 victims injured by what they suspected to be a blistering chemical agent. (On Friday, Iraq’s U.N. ambassador said there was “no evidence” of chemical weapons use in Mosul.)

President Trump has asked for recommendations to change the U.S. military’s rules of engagement, which are more restrictive than required by international law.

Col. John Dorrian, a spokesman for the U.S.-led coalition, said no changes had been made yet. “We continue to use our surveillance and intelligence capabilities to verify targets to the best of our abilities, and we continue to use precision-guided munitions,” he said.

Hussein al-Qaisi and his family fled the Dindan neighborhood two weeks ago because of intense shelling where counterterrorism forces were advancing. They escaped the city this week.

“We were in the middle of heavy fire,” he said, adding that a rocket had landed in his garden.

Nearby, civilians were being treated in a field clinic. “Make Mosul Great Again,” someone had scrawled on a blast wall.

Morris reported from Sulaymaniyah.