MOSUL, Iraq — For a time, the caliphate really did exist: a terrifying medieval prophecy sprung to life and captured in the pitiless freeze-frames of propaganda videos. Even as U.S.-led forces in Iraq and Syria deal decisive blows to the Islamic State, the group remains a potent threat.

In 2014, ensconced amid the looted bank vaults of Mosul and on the killing fields of Raqqa, the Islamic State was at the apex of its strength. From its twin bases in Iraq and Syria, it subjugated millions, dispatched operatives to strike the capitals of Europe, bestrode the cyberspace battlefield, and beheaded captive Americans and other foreigners whom the world’s mightiest militaries were powerless to pluck to safety.

Now an all-but-stateless Islamic State – largely driven from Mosul, and besieged in its self-declared capital, Raqqa – might seem poised for oblivion.

But longtime observers warn that the group’s virulent ideology is still very much alive, along with its ability to threaten both the immediate region and the wider world.

“It’s on its way to losing the caliphate, but that’s not the end of the story,” said Hassan Hassan, a fellow with the Washington-based Tahrir Institute, who has written widely about the group. “It’s a different story now, with a different plot.”

In May of last year, as U.S.-backed forces were seizing large chunks of the Islamic State’s territory, the group’s then-spokesman, Abu Mohammad Adnani – who would die a few months later in a fiery U.S. airstrike – shrugged off these losses as part of a divine plan that would leave the group ultimately victorious.

“Do you think, America, that defeat is the loss of a city or land?” he asked mockingly. “Defeat is to lose the will to fight … and you will only win, America, if you rip the Quran from our hearts.”

The nine-month battle of Mosul, where Iraqi forces are clearing out the last jihadist strongholds in the city’s western half, is all but done. But the price in what was once Iraq’s second city has been heavy, both in lives lost and the immense scope of destruction.

Swaths of Mosul’s storied Old City are now a denuded landscape. Railings and signs dangle from buildings reduced to crumbled masonry. Surviving civilians, still terrified, welcome coalition soldiers as liberators, but gesture – numbly yet insistently – toward ruins under which loved ones lie buried.

In the few structures still standing, remnants of the Islamic State’s rule still can be seen. Here, a scatter of charred religious pamphlets, one with a special prayer for patients healing from wounds and injuries. There, an explosives belt with its detonator parts scattered. On a battered wall, a smear of graffiti with a signature Islamic State slogan: “Baqiyah wa tatamadad” or “Enduring and Expanding.”

That would seem a tall order for the group at this juncture, but the Islamic State has made resilience its trademark.

Even before Iraqi Prime Minister Haider al-Abadi traveled to Mosul last week to proclaim victory in the fight to retake the city, the group already was reverting to its roots as an insurgency, melting into desert hinterlands on the Syria-Iraq border, launching hundreds of attacks from areas that had been deemed primarily pacified.

“They’ve got a playbook for this that they’ve used before,” said William McCants, a senior fellow at the Brookings Institution and the author of a recent book about the group. “They go to ground. In territory they don’t control, they try to blend in. They carry out assassinations and terror attacks, and prepare for a comeback.”

In Mansoura, a local leader named Sheikh Fawaz Beik helped negotiate a cease-fire and the withdrawal of Islamic State militants from the town in May. Last week, the sheikh was recovering from injuries from a remote-controlled bomb blamed on remnant fighters from Islamic State.

He described the determination of “Daesh” – an Arabic acronym the group considers pejorative – to cling to its emblems at any cost.

On the day of the cease-fire, he recounted, the fighters were asked to display green Islamic flags rather than their own black banners as part of their exodus from the town.

They heatedly refused, and amid the ensuing quarrel, one of the group’s fighters detonated a car bomb, setting off a firefight that left some 30 of the militants dead.

“They would not accept it,” the sheikh recalled. “They said, ‘There is only one flag in the world: the Daesh flag.’ “

Only subscribers are eligible to post comments. Please subscribe or to participate in the conversation. Here’s why.

Use the form below to reset your password. When you've submitted your account email, we will send an email with a reset code.