BEIRUT, Lebanon — Three years ago, a black-clad cleric named Abu Bakr al-Baghdadi ascended a mosque pulpit in the Iraqi city of Mosul and addressed the world as leader of a new terrorist state.

The announcement of the so-called caliphate was a high point for the extremist fighters of the Islamic State. Their exhibitionist violence and apocalyptic ideology helped them seize vast stretches of territory in Syria and Iraq, attract legions of foreign fighters and create an administration with bureaucrats, courts and oil wells.

Now, their state is crumbling.

In Syria, American-backed militias have surrounded Raqqa, the group’s capital, and breached its historic walls. Across the border, Iraqi forces have seized the remains of the Mosul mosque where Mr. Baghdadi appeared and besieged the remaining jihadists in a shrinking number of city blocks.

But the loss of its two largest cities will not spell a final defeat for the Islamic State — also known as ISIS, ISIL and Daesh — according to analysts and American and Middle Eastern officials. The group has already shifted back to its roots as an insurgent force, but one that now has an international reach and an ideology that continues to motivate attackers around the world.