Iraqis inspect the scene after a bomb explosion at an outdoor market in Baghdad on May 17, 2016. (Khalid Mohammed/AP)

As the Islamic State loses territory in Iraq and Syria, it is waging a counteroffensive far from the battlefield, taking its fight to the bustling markets and streets of the Iraqi capital in an attempt to distract security forces and create chaos.

The group has unleashed carnage in Baghdad over the past week, carrying out a wave of attacks ferocious even for a city so used to violence. Bodies of the dead lay scattered among blazing shops and vegetable stalls after militants launched four separate bombings in open-air markets. Checkpoints were also attacked.

U.S. and Iraqi military officials say the attacks are part of an effort by the Islamic State to stall any offensive to retake its strongholds in Iraq — Fallujah and Mosul — by refocusing security forces on the capital. While Iraqi commanders characterize the bombings as a sign of desperation, analysts say they are a calculated campaign to worsen instability in the capital amid a political crisis in Baghdad.

With its origins in al-Qaeda, the Islamic State has carried out mass-casualty bombings on civilian targets throughout its existence. But the severity of the latest bombings in Baghdad is “somewhat unprecedented” since the group declared its caliphate two years ago, according to Otso Iho, an analyst at IHS Jane’s Terrorism and Insurgency Center.

Over the past week, more than 150 people have died in violence linked to the group in Baghdad alone, a reminder that the Islamic State will be able to continue to wreak havoc even if it is eventually routed on the ground. The Islamic State-linked news agency Amaq claimed that the group had killed 522 people in the capital in less than a month.

Islamic State militants “may be reverting to their terrorist roots,” Gen. Joseph Votel, head of U.S. Central Command, told reporters Wednesday in Germany ahead of a visit to the Middle East. “We have to respect our enemies and respect their ability to adapt and adjust on the battlefield.”

The militants have lost about 45 percent of their territory in Iraq and 20 percent in Syria, according to the latest U.S. military estimates.

Iraqi forces have retaken ground along the Euphrates River in the western province of Anbar, most recently winning back the town of Hit. They have also advanced south of Fallujah, which has been under the group’s control for more than two years.

“They want to distract us and drag our forces that are now around Fallujah towards the capital in order to reduce pressure,” said Yousef al-Abadi, a spokesman for Baghdad operations command, which is responsible for security in the capital. “It’s their last card.”

As Islamic State militants bombed three markets on Tuesday, commanders said the group’s forces were collapsing 240 miles west in Rutbah, a smuggling town deep in the desert that is a waypoint between Iraq, Jordan, Saudi Arabia and Syria. Iraq’s Defense Ministry announced that it had seized control of the town’s local council building on Wednesday night.

“It’s not the same enemy we fought in Ramadi or Baiji,” said Brig. Gen. Abdul-Ameer al-
Khazraji from Iraq’s elite counterterrorism forces, referring to battles for the province’s capital and the country’s biggest oil refinery.

“They are just leaving their weapons and running away,” he said.

But Hisham al-Hashimi, a Baghdad-based analyst specializing in jihadist movements, said the Islamic State is cutting its losses in some areas — withdrawing from outposts that are difficult to defend like Rutbah in an effort to prevent casualties.

Instead, it is doubling down to defend its more valuable assets, including Mosul and Fallujah in Iraq and Raqqa and Deir al-Zour in Syria, he said.

The group is holding up better militarily in Syria, where it benefits from a more complicated web of groups fighting one another on the ground, he added.

There, the Islamic State is also attempting to strike back. It blew up the Shaer gas field on Saturday and has been making advances toward the ancient town of Palmyra, which was recaptured by the Syrian army in March. Also on Saturday, the militants launched their first offensive in many months against the Syrian army in the eastern city of Deir al-Zour, though reports from there say the battle has stalled.

The Islamic State has attempted counteroffensives on the battlefield in Iraq, too, but has been largely unable to gain new ground. Kurdish commanders said hundreds of Islamic State fighters were killed as they tried to capture Teleskof in northern Iraq this month, an attack during which a Navy SEAL also died. Those front lines hadn’t moved significantly for more than a year and a half, but the attack came as Iraqi army forces were beginning to build up for a Mosul offensive farther south. Commanders say they are seeing an increase in hit-and-run attacks with no apparent strategic purpose other than to kill.

“They are not able to generate the large attacks that we’ve seen in the past,” said Maj. Gen. Gary Volesky, commander of the land component of the U.S.-led coalition’s campaign against the Islamic State.

“Every time the Iraqis are able to put pressure on Daesh, they delay,” he said, using the Arabic acronym for the militant group. “They trade space for time.”

By disrupting Baghdad they may be able to gain time.

The frequency of attacks since the beginning of the year suggests a “concerted campaign, rather than the last resort response of an organization cracking under pressure as it faces eradication,” Iho wrote in an analysis. Political reconciliation between Sunnis and Shiites — which U.S. officials stressed was necessary alongside a military offensive in order to eradicate the group — never got off the ground. Splits within the Shiite community itself have also surfaced.

Even before supporters of the Shiite cleric Moqtada al-Sadr stormed Baghdad’s Green Zone last month, the commander of the Mosul operation, Maj. Gen. Najim al-Jabouri, expressed concern that the capital’s political crisis was holding up efforts to retake the city.

Since then, two bombings have targeted markets in Sadr City, a support base for the cleric in eastern Baghdad, accounting for the majority of the deaths in the capital over the past week.

Hashimi said that’s a calculated move by the Islamic State to push Sadr’s supporters, already frustrated with the government, to cause further unrest.

Last week, hundreds of protesters gathered at the bomb site in the poverty-stricken Shiite neighborhood. The slogans on their placards didn’t direct blame at the militants.

“It wasn’t Daesh that killed them,” said Jassim al-Assadi, 24, who lost two friends in the bombing. “It was the government. If the government really wanted to protect this city, they would deploy forces to protect us, not just leave us to die.”

Mustafa Salim in Baghdad and Liz Sly in Beirut contributed to this report.