Shortly​ before the siege of Raqqa began in June, Islamic State officials arrested Hammad al-Sajer for skipping afternoon prayers. Hammad, who is 29, made a living from his motorbike: he carried people and packages, charging less than the local taxis. IS had arrested him a number of times before – mostly for smoking cigarettes, which were banned under IS rule – but he had always been released after paying a fine or being lashed. Attendance at prayers was compulsory and he had missed the Asr, the afternoon prayer, because a passenger had made him wait while he went into his house to get money for his fare after a trip to Raqqa’s old city. Hammad expected to be fined or lashed, but this time he was sentenced to a month in prison. Except it turned out not to be prison. On his first morning, ‘militants blindfolded us and took us in a vehicle to a place that seemed to be inside the city because it took no more than ten minutes to get there.’

Hammad and the other prisoners, all of them local men, were taken to an empty house. In one of the rooms there was a hole in the floor. Rough steps led down about sixty feet before the tunnel flattened out into a corridor, which was connected to a labyrinth of other tunnels. A fellow prisoner, Adnan, told Hammad that IS had started work on what was effectively a subterranean network a year and a half earlier. In other words, construction began in 2015, after IS’s spectacular run of victories ended and it started its long retreat in the face of Kurdish offensives backed by coalition firepower. To escape the aerial bombardment, IS decided to disappear underground, digging immense tunnel complexes underneath its two biggest urban centres, Mosul in Iraq and Raqqa in Syria, to help it defend itself when the final assaults came.

Few people in Raqqa knew the extent of the excavations going on beneath their feet – not even Hammad, who rode his motorbike around the city every day. The entrances were always in districts from which local inhabitants had fled or been evicted. ‘When we got into the tunnels we were amazed,’ Hammad remembers. ‘It was as if an entire city had been built underground.’ IS must have needed an army of workers to build it – but then there were large numbers of prisoners and jobless labourers to draw on. The prisoners were told as little as possible about what they were doing: anyone who asked a lot of questions was punished. Hammad saw rooms with reinforced concrete walls and ceilings, and what looked like boxes of ammunition piled up on the floor. When he asked about the boxes, he says, one of the guards ‘hit me on my back with a piece of cable and said: “Don’t poke your nose into things. This is not your business. Do your job and keep quiet.”’ The foreign fighters on duty were silent and unapproachable, but some of the guards were locals and occasionally talked to the diggers during the ten-hour working day. ‘Sometimes they joked with us because they were bored and tired,’ he says. One day he asked one of them what all this hard work was for. ‘This great construction will help the lions of the caliphate to escape,’ he said (the ‘lions’ were the IS emirs and commanders). ‘They have a message to deliver to people and they should not die too soon.’

IS officials used prisoners to work on the tunnels when they could, but they also hired labourers. One of these was Khalaf Ali. When IS seized the city in 2014, he was selling cigarettes in the street. ‘I was picked up by some militants who took me to a commander,’ he says. ‘They did not take me to prison, but they confiscated my boxes of cigarettes and said that if I sold cigarettes again, they would put me in prison and I would get thirty lashes.’ He started spending his days in a local square with other unemployed men; they would wait for a car or truck to stop and offer them odd jobs – moving furniture, mending broken doors or windows. In April 2016, Khalaf was sitting in the square with the others when an IS security man said he wanted to talk to them. At first they were nervous, but the official said they could have work if they registered their names at an IS office. When they showed up at 7 a.m. the following day, they were told they had to agree to certain conditions: ‘We must not talk about what we were doing in public as it was one of the caliphate’s secrets and, if we violated this condition, they would kill us as traitors.’ They were blindfolded and driven a short distance to an empty house, where the blindfolds were removed. It wasn’t the house Hammad had first been taken to: here, there were no stairs, just a sloping tunnel about 150 feet long, which took them around sixty feet underground.

‘We entered an area that looked like a residential complex,’ Khalaf remembers. ‘There were many rooms: some under construction, others finished, with concrete walls.’ The labourers used trolleys to move the excavated soil to a certain point in the tunnel, where other men, whom he didn’t know, took charge of moving it up to the surface to be dispersed. The various teams of workers were forbidden to talk to one another. Khalaf’s team was responsible for moving furniture, including sofas and beds, to completed rooms. ‘There was electricity, though not in every room, and the corridor had lights.’ As the Kurdish-led Syrian Democratic Forces (SDF) advanced towards Raqqa and the American bombardment intensified, IS doubled wages from $4 to $8 a day, though money was deducted if the IS officials were dissatisfied with the quality of the work. Once Khalaf asked an IS militant who had first had the idea of building these underground complexes. He was told that it was ‘our brothers in faith’: ‘in Afghanistan,’ the militant said, ‘many attacks were repelled and failed because of the tunnels.’

Hammad and Khalaf, who didn’t know each other, escaped separately from Raqqa during the first weeks of the siege. Hammad fled in the early morning with a group of men, moving from house to house whenever there was a break in the fighting. They took advantage of a tactical retreat by IS fighters to run towards the Kurdish-led forces, stopping every ten minutes to hide behind walls and the wreckage of houses until they reached safety. The SDF questioned them to make sure that they weren’t IS infiltrators, and then took them to a camp for displaced people at Ain al-Issa, north of Raqqa. Khalaf’s journey out of Raqqa was even riskier: after a number of people in his neighbourhood were killed, he and thirty others decided to try to escape from the city guided by SDF radio broadcasts. Even so, Khalaf says, they were sometimes trapped inside a house by the fighting for several days. ‘Finally we fled, but we lost some of our friends,’ he says. ‘We saw their bodies lying there as we ran, but everybody was afraid of snipers so we couldn’t go back for them.’

The siege of Raqqa, a small city on the Euphrates with a population before the war of less than 300,000, has now been going on for four months. IS fanaticism is one reason it hasn’t fallen sooner, but that alone wouldn’t be enough to stop the SDF, with the might of American airpower behind it. The network of tunnels connecting up bunkers, hideouts and hidden escape routes is the key to the resistance. IS fighters are able to move swiftly underground, shifting positions before they can be detected and eliminated by bombing or shellfire. As in Mosul, the only way for the attackers to advance without sustaining heavy losses has been to call in coalition airstrikes so intense that much of the city has already been destroyed. The Kurdish general commanding the SDF, Mazlum Kobane, was quoted on 26 September as saying that his forces now hold 75 per cent of Raqqa, but there are still some 700 IS fighters and 1500 pro-IS militiamen in the city centre. They can probably hold out for weeks or even months, using the same skills in urban warfare that IS demonstrated during the siege of Mosul. A combination of snipers, suicide bombers, mortar teams, mines and booby traps slow down and inflict damage on the enemy. For IS, eventual defeat is inevitable, but they remain dangerous: last month a group of IS fighters in SDF uniforms killed 28 SDF men in a surprise attack.

Still, tactical agility won’t be enough to save the caliphate, which is now being overwhelmed on multiple fronts. Islamic State’s great strength came from the way it combined religious cult and war machine; its weakness was that it saw the whole world as its enemy, which meant that it would always be outnumbered and outgunned. Without allies and dealing only in violence, it led an unlikely alliance of states normally hostile to one another to find common cause against it, and engage in a degree of reluctant co-operation. As IS comes close to losing its power, old rivalries and divisions are beginning to re-emerge – but in a political landscape significantly reshaped by the war with IS.

A decision IS took three years ago – after its columns had won speedy victories over the Iraqi and Syrian armies and captured much of eastern Syria and western Iraq – helped to set the stage for the next phase of the conflict: instead of keeping up the pressure on the demoralised forces of the central governments in Baghdad and Damascus, IS diverted its forces to make war on the Kurds in both Iraq and Syria. In August 2014, it launched a surprise attack on the Iraqi Kurds which almost reached their capital, Erbil, and in September it started a prolonged assault on the Syrian Kurdish city of Kobani. Quite why IS did this remains a mystery: it’s possible that it was acting with the encouragement of Turkey, which was alarmed by the growing strength of the once marginalised Syrian Kurds. Whatever the explanation, the Kurds in both Syria and Iraq unexpectedly found themselves, much to their political benefit, in the front line of an international campaign against IS. The Peshmerga in Iraq and the People’s Protection Units (YPG) in Syria were suddenly awarded support from the most powerful air force in the world. Turkey had been prepared to see Kobani fall to IS, but the city was saved by intense US airstrikes, though 70 per cent of its buildings were left in ruins. The US had been desperately seeking reliable ground troops in Syria and found them in the YPG, which took control of much of the southern side of the Syrian-Turkish border. In Iraq, the Kurds used the defeat of the Iraqi army in northern Iraq at the time of the fall of Mosul to seize the ‘disputed territories’ outside the boundaries of the Kurdistan Regional Government (KRG), thereby expanding the Kurdish-controlled area by 40 per cent.

Kurdish leaders​ in Syria and Iraq have long wondered, mostly in private, whether they would be able to retain their political and military gains once IS was on the road to defeat. Early last year, Muhammad Haji Mahmud, a senior Peshmerga commander, told me that the war with IS had brought great benefits to the Iraqi Kurds: ‘We have become a regular army, rather than a guerrilla force; are supported by US and European air power; can buy weapons openly; and are praised internationally for fighting terrorism.’ His big fear was that the Kurds wouldn’t ‘have the same value internationally’ once Mosul was liberated and IS defeated. Nor did he think they would have the strength to hold onto the disputed territories without international support. In Syria, too, Kurdish leaders worry that they are over-extended and too reliant on the Americans, who may stop supporting them diplomatically and militarily once Raqqa and the last IS strongholds have fallen. Turkish intervention is one threat; another is the Syrian army, which – with Russian air cover – has surrounded IS at Deir Ezzor and will now want to advance to take the oilfields further east. Developments in Iraq and Syria often mirror each other: the Syrian army captured East Aleppo in December 2016 and the Iraqi army took Mosul seven months later. The trend in both Iraq and Syria has been for the military power of the central government to bounce back – which poses a mounting threat to Kurdish separatism.

Now that the outcome of the war with IS is no longer really an issue, the conflict in the region is turning towards confrontation over the powers, and even the existence, of the two Kurdish quasi-states: the Kurdistan Regional Government (KRG) in Iraq, and what the Syrian Kurds call Rojava, in the north-east corner of Syria. In 2012, following the uprisings of the Arab Spring, the Syrian army withdrew from Kurdish cities and towns in the area, and what to all intents and purposes is the Syrian branch of the Kurdistan Workers Party, the PKK, which had been fighting a guerrilla war in Turkey since 1984, took over. As a US military ally against IS from 2014, the Syrian Kurds field an army about 50,000 strong. Their fighters are now moving into eastern Syria, where they will confront advancing troops of the Syrian army. A collision is probable and its outcome uncertain, but for the Kurds it is fraught with danger, since they can’t know what American policy towards them will be under Trump.

However, the first real post-IS crisis for the Kurds has come not in Syria but Iraq. The non-binding referendum on independence for the Iraqi Kurds held on 25 September was opposed by the UN, US, UK, France and Germany as well as by regional powers including Turkey, Iran and Iraq. The last three promised retaliation against the KRG if the vote was held, a threat that its president, Masoud Barzani, interpreted as a bluff. It turned out to be a very real threat and within days of the referendum, Baghdad had closed Iraqi airspace to international flights out of Erbil and Sulaimaniyah. The Iraqi government held joint military manoeuvres with Turkey and Iran and is threatening to take control of the KRG’s borders. The Kurds’ overwhelming vote for their own independent state has had the effect of highlighting the scale of the obstacles to their self-determination. Iran, Turkey and the Iraqi government are now united as never before and in a position to enforce a blockade on the KRG; there is a limit to what the Kurds can do by way of retaliation.

‘The Kurdish leadership in Iraq doesn’t really have any military or diplomatic options,’ says Omar Sheikhmous, a veteran Kurdish leader who believes the KRG has badly overplayed its hand. Barzani’s decision to hold a referendum – driven primarily by intra-Kurdish political divisions – may come to seem a serious miscalculation. Before the poll, Barzani rejected compromise proposals from the US and its European allies while Kurdish leaders underestimated the likelihood of Turkey and Iran following through on threats that had proved hollow in the past. Sheikhmous had warned me before the vote that the Kurdish leadership in Iraq could be about to ‘throw away all they had won over the previous twenty or thirty years’. He compared the referendum with other classic blunders in Iraqi history, such as Saddam Hussein’s invasion of Kuwait in 1990, when he entirely misjudged how other powers would respond. KRG leaders wrongly believed that Turkey and Iran wouldn’t want to jeopardise their sizeable economic interests in Iraqi Kurdistan by objecting to the vote. But neither Turkey nor Iran can countenance the prospect of independence for Iraqi Kurds since it would inflame their own Kurdish minorities. Turkey’s reaction was especially hostile: the Kurds, Erdoğan said, ‘are not forming an independent state, they are opening a wound in the region to twist a knife in’. Barzani had cultivated good relations with Erdoğan, who now says that the relationship is finished: the KRG, ‘to which we provided all support, took steps against us, it will pay the price’. In future, he says, Turkey will deal only with the Iraqi government. Iraqi Kurds are hoping the US will once again come to their rescue by mediating between them and their opponents: they say the roads into Kurdistan are still open and that nothing has yet changed on the ground. The KRG may survive its present isolation but the risk is growing that the Kurdish quasi-states will go the same way as the caliphate.

6 October