United States-backed fighters retook the final enclave of the Islamic State “caliphate” on Saturday in Baghuz, eastern Syria. With thick beards and wearing kaftans, Isis militants continued to emerge from underground tunnels over the weekend, surrendering to the Kurdish-led Syrian Democratic Forces (SDF).

Unlike Donald Trump, who has already announced the downfall or imminent destruction of Islamic State many times, the SDF spokesman carefully described the “100% territorial defeat of Isis”. Certainly, this turn of events will force a step change for Isis, but it is one that it is prepared for.

Ideologically, it became fixated on moral purity and, as a consequence, murdered tens of thousands of Syrians and Iraqis

As a political structure, the “caliphate” was fundamentally unstable. It was defined by a savage propaganda of slavery, beheadings and crucifixion, much of which contributed to a fair deal of internal disquiet. There were scores of executions of even top commanders and ideologists for dissent or disloyalty. The Isis leader, Abu Bakr al-Baghdadi, was said to have survived a coup attempt by a group of north African fighters as recently as January. But Isis had emerged out of a bloody and bitter dispute with al-Qaida in 2014 – they continue to execute each other’s fighters – and retained within its leadership style an ingrained, almost compulsive, proclivity for excommunication.

Isis replaced victimhood with bloodlust. Ideologically, it became fixated on the search for moral purity within the territories it controlled and, as a consequence, murdered tens of thousands of Syrians and Iraqis, including the mass killing of fellow Sunni Muslims for “apostasy”. It readily publicised its killing machine. At the time of the SDF offensive, Isis was heading towards the sort of jihadist bankruptcy that Osama bin Laden once cautioned against.

The collapsed caliphate could now force Isis to reset, and “correct” course. The shift in the power balance enables it to reclaim the mantle of victimhood and, at the same time, reimagine its past as a utopian enterprise. This myth-making will inevitably boost its efforts to refocus on the main target of global jihad: the west. It is only helped along by far-right extremism of the sort behind the attack in New Zealand, which makes newly relevant a large part of mainstream jihadist discourse about Muslims under siege, even in the west.

Isis ideology has always interacted with the historical moment. Amid the violent contest for power in post-invasion Iraq, Isis’s antecedent groups broke with Bin Ladenism in 2005 and focused on anathematising Shia Muslims. They also reinvented jihadism as a state-building project on account of their tactical alliance with former officials from Saddam Hussein’s state, and the urgent need to seize and hold territory.

When, in 2010, these groups were looking down the barrel of strategic defeat, a cultish strain emerged, fixating on the apocalypse and waiting for the arrival of the messiah. Now, with the uprooting of the caliphate, we can expect a strong nostalgic motif that yearns for – and seeks to avenge the fall of – an idealised, just society.

This shape-shifting will continue for as long as the operating environment for Isis is marked by despair and sweeping trauma. In Iraq, the state has continued to brutalise Sunnis, denying them the right of human dignity and the rule of law. In Anbar province, Sunni tribesmen armed to fight Isis now refuse to surrender their weapons, fearing state-linked Shia militia. Just as Isis was said to have thrown gay men from rooftops, so Iraqi security forces allegedly hurl prisoners from cliffs. Like Isis, they are reported to film themselves extracting confessions under torture, burning captives alive and beheading children.

The rise and fall of the Isis 'caliphate' Read more

It is unlikely Iraq will turn the page on its recent history of toxic sectarianism, as the leaders of notorious Shia security forces such as Asa’ib Ahl al-Haq and the Badr Organisation are now embedded in the parliamentary system. Baghdad is also pushed further towards the embrace of Iran by Trump’s disengagement from the region, as symbolised by President Rouhani’s tour of Iraq this month.

The US drawdown in Syria leaves it with even less influence over the shape of a postwar settlement, which will be led by Russia, Iran and Turkey, and is sure to amount to enforced surrender on the part of Bashar al-Assad’s opposition. Beyond that, the fate of an exhausted, abused and grieving generation of Sunni Syrians is uncertain.

Isis has already changed skins. It announced the switch to guerrilla tactics last July, when its fighters emerged from desert hideouts to massacre more than 200 people in the Druze community of Sweida, in southern Syria. It has resurfaced in the Anbar and Nineveh provinces of Iraq, conducting car bombings and night operations in rural villages. There is speculation about a return to Mosul for hundreds of fighters pushed out of Syria. There are local press reports of urban sleeper cells, and cash and weapons buried in the sand. Meanwhile, across the region, Isis affiliates are assertive in Egypt, Libya and Afghanistan, and are spreading farther afield, from the Philippines to Burkina Faso.

Baghuz is certainly not the last gasp for Isis in Iraq and Syria, but rather a sharp intake of breath before it goes underground. For years the organisation has prepared for this scenario: ultimately, a return to its guerrilla roots. It may well benefit from a fresh narrative, grounded in nostalgia rather than bloodlust. It will also gain some strategic advantage from the rise of the far right in the west, and most importantly, from the failure in Iraq, Syria and elsewhere, to offer a more just, law-based alternative on the ground.

• Alia Brahimi is co-founder of Legatus Global and a former research fellow at Oxford University and the LSE