US-backed forces declared the final defeat of the Islamic State's self-declared caliphate Saturday, bringing to an end the brutal experiment in state building that had lured foot soldiers from around the world and inflicted unimaginable suffering on those caught up in the militants' rampage through Iraq and Syria.
The Syrian Democratic Forces hoisted their yellow flag atop a bullet-scarred building in the eastern Syrian village of Baghouz, replacing the remaining black flag to fly over the speck of land where the most die-hard of the militants had fought their last stand.
On his Twitter account, SDF spokesman Mustafa Bali declared the "total elimination" of the Islamic State's territorial control, four-and-a-half years after the group's sweep through Iraq and Syria drew the United States into the war against it.
The end of the caliphate won't mean the end of the Islamic State, US officials and analysts warn. As their territorial defeat neared, the militants switched gears and began regrouping as an insurgency that is already destabilising areas from which they were driven out years ago.
But it was nonetheless a landmark moment, hard-won and at a heavy price.
Tens of thousands of people lost their lives in massacres and executions committed by the militants, in the battles to dislodge them and in the airstrikes that provided the muscle for the fight. Thousands of women from the Yazidi religious minority were enslaved, and many still are missing.
Victims say they may never be able to shake the memories of the horrors they endured.
"If you had told me the Islamic State would end, after all this time and all these killings, I would never have believed you. Maybe I still don't," said Mahdiya, 28, a member of Iraq's Yazidi minority who lived nearly five years as a slave.
"They come to me in my dreams; they come to me when I close my eyes," she said. "They took so much from all of us that I wonder if we'll ever feel truly free here."
The SDF said 11,000 of its fighters died battling the military in Syria. Although the Iraqi security forces and militias that fought the group in Iraq have not released casualty figures, it is estimated that at least an equivalent number of their forces were killed.
The war had cost the United States $28.5 billion as of December, according to the Pentagon, and a total of 16 US soldiers were killed in action, among 72 who died while serving with Operation Inherent Resolve, as the campaign was called.
The loss of its territory was a crushing blow to the vaunted ambitions of the Islamic State, which at its peak controlled an area the size of Britain and wielded an army of as many as 100,000 men.
"You will conquer Rome and control the world" Islamic State leader Abu Bakr al-Baghdadi promised his followers in July 2014 when he proclaimed the creation of the so-called caliphate at the Great Mosque of al-Nuri in Mosul.
On Saturday, all that remained of his boast was the smoking pile of shredded tents, bombed-out vehicles and crumpled corpses strewn around the fields of dusty Baghouz, according to footage taken by journalists escorted there by the SDF.
The unexpectedly fierce battle for Baghouz made clear the danger that the Islamic State might yet retain considerable capabilities.
The fight proved far tougher than had been anticipated, because there were far more fighters holding out in the village than the US military had thought. In January, Pentagon officials had put the number there at around 2,000. Last week, the SDF said it had killed 12,000 and detained more than 500. In addition, more than 72,000 civilians escaped the area, more than 10 times the 7,000 that aid workers had been told to expect.
The US military estimates that it killed 70,000 out of about 100,000 Islamic State fighters, a figure that appears to have given rise to calculations that there are 30,000 Islamic State fighters still lurking in liberated terrain. US military officials say that number is too high, though they acknowledge that all of the figures are approximations.
There is no doubt, however, that the Islamic State remains a potent threat to the stability of Iraq and Syria, military officials and analysts say.
"Isis isn't going to launch any big surprises anytime soon, but what remains of it after the caliphate will still be a huge challenge," said Hassan Hassan, who studies the group at the Washington-based Center for Global Policy. "If efforts against them succeed, they'll always be small. If they don't, you're looking at something like another Taliban, with them effectively controlling large areas at nighttime and being able to reach any person in the towns or villages."
In Iraq, where the organisation originated, the militants have already regrouped as a rural insurgency in areas north and east of Baghdad and have been carrying out regular assassinations and bombings.
In Syria, there have been fewer signs of an organised revival, but officials caution that may not last.
"Currently, Isis is regenerating key functions and capabilities more quickly in Iraq than in Syria, but absent sustained [counterterrorism] pressure, Isis could likely resurge in Syria within six to twelve months and regain limited territory," said a report submitted by the Pentagon's inspector general to Congress in February.
President Donald Trump, who had hailed the defeat of the caliphate on Thursday, pledged in a statement to remain "vigilant" against an Islamic State return by "aligning global counterterrorism forces."
"While on occasion these cowards will resurface, they have lost all prestige and power. They are losers and will always be losers," he said, adding a warning to young people who might be tempted by the group's propaganda: "You will be dead if you join. Think instead about having a great life."
US military officials have also warned that the uncertainty surrounding Trump's plans for a troop drawdown and eventual withdrawal from Syria could create a security vacuum within which the Islamic State could regroup.
Many analysts blame the vacuum left by the departure of US troops from Iraq for the swift regeneration of the al-Qaeda insurgency that eventually became the Islamic State. The rebellion in neighboring Syria that saw state authority collapse across vast areas also played a part, giving the Iraqi-based militants space in which to regroup, organise and prepare for their sweep through both countries.
The US-led military campaign began in August 2014 after the militants swept through Iraq, trapped thousands of helpless Yazidis on a mountain and threatened the US-allied Kurdish administration in the north.
The militants described the land that they seized as an Islamic state, and it often bore some hallmarks of an actual state. Bureaucrats dealt with household bills and garbage collection. The group minted its own coins. It also enforced harsh laws that included executions and beheadings for relatively minor offenses.
The end of the war will bring into focus the vast challenges that lie ahead. Tens of billions of dollars' worth of damage was inflicted on the economies and infrastructure of Iraq and Syria. Towns, neighbourhoods and villages have been levelled, with little hope that they will be repaired anytime soon. More than 5 million people fled their homes, and at least 2 million are still displaced, many because they don't have homes to go back to.
The failure to rebuild and get people home is already fueling the kind of grievances and social cleavages that drove the Islamic State's rise, military officials and human rights groups warn.
Prisons are full to the brim with suspected fighters, including hundreds of Westerners whose governments refuse to repatriate them.
But this was a moment to relish victory. At a ceremony held by the mostly Kurdish SDF at their Omar oil field base near Baghouz, musicians in gold epaulets played "The Star-Spangled Banner" and the Kurdish national anthem and paid tribute to those who had sacrificed their lives.