Beirut

As I write, Isis is still holding out on a few hundred square yards of dirt in the village of Baghouz in Syria. This is all that remains of a ‘caliphate’ that was once almost half of Syria and a third of Iraq. The fighting has now gone on twice as long as the battle for Mosul, a city of a million and a half.

Isis has just sent a taunting message to President Trump, saying he spoke too soon when he tweeted: ‘We have defeated Isis in Syria.’ But they know that the end is inevitable and coming fast. An Isis video apparently filmed in Baghouz shows people squatting in the open next to cars and trucks packed with furniture and blankets. A man asks the camera: ‘What is our fault? What is our crime? Why are we being bombed? Why have all infidel countries mobilised against us while the rest of the world keeps silent?’ He answers his own question: ‘Because we wanted to implement God’s Sharia law.’

The crusaders’ jets are overhead. The ‘Kurdish atheists’ are down the road. The air is filled with the crack of rifle fire and the crump of artillery. The video carries the — hardly reassuring — message that victory and defeat are not measured in this world but in the rewards to come in the afterlife.

And good riddance, you might say: the caliphate of the snuff video and the sex slave had it coming. One piece of news video captures this moment: pictures from the ‘liberated’ territory outside Baghouz. Hollow-eyed Yazidi boys stand by the side of the road, unsure of what to do. They were wrenched from their parents’ arms when they were hardly old enough to cross the road on their own, and sent to Isis indoctrination camps. There, they were beaten and told that Isis was their new family. But they were disposable to the jihadis, and used to plant roadside bombs or carry out suicide attacks. Many have forgotten their own language; long after they are home, some will still parrot the Isis slogans they were forced to learn in captivity. This is heartbreaking, but the Yazidis are free now; a genocide has been stopped.

For all that, though, it is important to understand that the disappearance of the Islamic State’s territory is not the disappearance of the Islamic State. The caliphate is gone but not the people who made it.

They are a mixed bunch: some true believers, some who joined for the money or because the local sheikh swore allegiance; some Iraqis and Syrians; some foreigners attracted by the Islamic State’s message. Baghouz has probably taken so long to fall because the Islamic State’s willing martyrs are what’s left now. It is also true that many of those still there are simply too poor to pay smugglers to sneak them out or too frightened to try. Getting caught would mean a certain bullet to the back of the head, as one white Canadian convert to Isis said after he made it to Kurdish territory. A great many of the foreigners didn’t like the Islamic State when they got there, but couldn’t leave. One Swedish woman, another convert, told a Kurdish TV crew: ‘I miss going to the supermarket with my family. I just want to go home and live a normal life.’

Of course, there are fanatics. Even as they surrender to Kurdish soldiers, some of the Isis wives raise the single index finger that symbolises one God, one caliphate, and the destruction of all enemies. ‘God is great. The Islamic State will never die.’ In more arresting images from the frontline, a woman in a niqab gathers her children around her and says: ‘We left so that Allah can give us another generation to fight jihad.’ She was called Um Nuh and was praying for God to grant her husband the ‘honour of martyrdom’.

At the nearby camp set up by the Kurds, al Hol, the manager shrugs and tells reporters that the foreign women are the worst. He’s had to separate them from the Syrians in the camp. ‘They’re ferocious. They burn the Syrians’ tents and call them kaffirs, unbelievers. They think they’re the only true Muslims.’

Perhaps the fallen ‘caliph’, Abu Omar al-Baghdadi, will send waves of suicide bombers back to western countries. Or perhaps — as with the ‘lone wolf’ attacks in the West — this will be more a matter of individual motivation. Um Nuh, the woman who wanted to breed the next generation of mujahedin, was Belgian, but she didn’t want to go back because Belgium had been part of the coalition against Isis. ‘They killed our women and kids… We need our own land.’

Of those who do return, a Danish diplomat I know told me that his government’s experience showed that nine out of ten wanted a quiet life. Only one out of ten posed a threat. The trick was to identify which was which. The British government believes that 900 men and women left the UK to go to the caliphate. Around 200 died there. If — using the Danish estimate — a tenth of those that are left were to come home wanting revenge for the caliphate’s destruction, that is an obvious problem.

In Syria and Iraq, Isis will endure because Sunnis there still have reason to hate and fear their governments. In Syria, many have disappeared into the regime’s jails for years; whether they’re alive or dead, no one knows. In areas that were under rebel control, the whole Sunni population know they are viewed with suspicion by the regime. In Iraq, too, Sunnis believe that the country’s draconian security laws are directed against them. They bitterly resent the security forces and view Shiite militias as death squads. This is fertile soil for Isis. They have been here before, in 2011, when they were driven into the Iraqi desert and Baghdadi said they could not stay on the same piece of ground ‘for even 30 minutes’. With the arrival of the Arab Spring, they seemed irrelevant. But having far fewer numbers than remain today, they clawed their way back, with tactics they will use again now. They may — in truth and despite their early conquests — have been incompetent soldiers, but they are good insurgents and really great terrorists.

So in Iraq, they have been waging a campaign of assassinations to pry Sunni villages loose from government control. One report spoke of a loyal sheikh taken from his home after dark and executed in front of the whole village. The security forces were back in their base, nowhere to be seen. In large areas of the countryside, even those Sunnis who might want to support the government know it cannot protect them: the night belongs to Isis. For that reason, President Trump was premature to say that Isis had been defeated. In another way, he was right: tens of thousand of Isis fighters have been killed, and if Isis has not been destroyed entirely, it has been crippled.

With no defined territory to bomb, American air power will be less effective against the remnants of Isis than the local security forces. The risk is that the next 9/11 might come from the ‘ungoverned space’ either side of the Euphrates in what was Isis territory in Iraq and Syria. That’s why the US military wants to stay to help the Kurdish militia in Syria and the government forces in Iraq. But this starts to look very much like nation building, with American troops on the ground indefinitely, albeit in relatively small numbers. Trump wants no part of that. His tweet was partly to pre-empt his own generals.

After nearly five years of the caliphate, the appeal and the success of Isis — and therefore the key to its permanent defeat — remains a mystery. The majority of the Sunni population it ruled over were revolted by its brutal theatricality: throwing gays from rooftops, stoning ‘fallen’ women, beheading apostates — apostasy being whatever Isis said it was. Five years ago, the idea of reviving the medieval practice of slavery would have seemed outlandish. Al Qaeda never dared — or wanted — to attempt this, but Isis did it.

Yet Isis is not a throwback to the Prophet’s time or any other time in Islamic history. It is modern, Leninist even, in its understanding of the creative use of violence to reshape society. Documents left behind in the retreat from other parts of Syria show the blueprint for taking over a town, starting with the murder of prominent local leaders — doctors, moderate clerics, intellectuals, journalists — to sow fear and panic. The Isis creed was set out in The Jurisprudence of Blood, a book that can sometimes be glimpsed in its videos. It was written by one of al Qaeda’s leading theologians, in Afghanistan after the anti-Soviet jihad, but was not adopted by al Qaeda. It provides justification for suicide bombings and killing civilians, including women, children and the elderly, ideally by beheading them.

Arabs with a long history in the jihadist movement and who knew this book were joined in the caliphate by new western recruits. I remember a shop on the Turkish border with Syria that used to sell black flags, Tasers and machetes in black steel with long, curved blades. These were not bought by locals: they did their beheadings by drawing a small, sharp knife across the throat, as they were used to doing in the ritual slaughter of sheep.

But the machetes started turning up in execution videos anyway; the Islamic State’s newest members thinking that beheadings ought be done with something that looked like a sword. Isis is a marriage between western imaginations formed by videogames and horror films and a recurring stream in Salafi ideology that represents itself as a purer, more authentic version of Islam. Both tend towards extremes. Both have lasting appeal to a minority within a minority. Isis, whatever name it goes by in future, will be with us for a while yet.

Many of those still in Baghouz are simply too poor to pay smugglers to sneak them out or too frightened to try

Paul Wood is a BBC correspondent.