BAGHOUZ, Syria -- U.S.-backed forces declared military victory over the Islamic State group in Syria on Saturday after liberating the last pocket of territory held by the militants, marking the end of a brutal self-styled caliphate the group carved out in large parts of Iraq and Syria in 2014.

The nearly five-year war that has devastated cities and towns across north Syria and Iraq ended in Baghouz, a minor border village where the cornered militants made their last stand, under a grueling siege for weeks.

On Saturday, the Syrian Democratic Forces raised their bright yellow banner from a shell-pocked house where the militants once flew their notorious black flag. Below it stretched a field shattered by the battle, pitted by trenches and bomb craters and littered with scorched tents, twisted wreckage of burned out vehicles, unspent explosives and few remaining corpses.

"Baghouz is free and the military victory against Daesh has been achieved," tweeted Mustafa Bali, a spokesman for the Kurdish-led SDF, referring to ISIS by its Arabic acronym.

The fall of Baghouz brings to a close a nearly 5-year global campaign against the Islamic State group that raged in two countries, spanned two U.S. presidencies and saw a U.S.-led coalition unleash more than 100,000 strikes. The campaign has left a trail of destruction in cities in Iraq and Syria, likely killed tens of thousands and drove hundreds of thousands from their homes.

The campaign put an end to the militants' proto-state, which at its height four years ago was the size of Britain and home to some 8 million people. But the extremist group still maintains a scattered presence and sleeper cells across Syria and Iraq. It's not known whether the group's leader, Abu Bakr al-Baghdadi, is still alive or where he might be hiding.

ISIS affiliates in Egypt's Sinai Peninsula, Afghanistan and other countries continue to pose a threat, and the group's ideology has inspired so-called lone-wolf attacks that had little if any connection to its leadership.

The "caliphate's" end also marks a new phase in Syria's civil war, now in its ninth year. The country is carved up, with the Iranian- and Russian-backed government of President Bashar Assad controlling the west, centre and south, the U.S.-backed Kurdish-led forces holding the north and east, and Turkish allies controlling a pocket in the north. The fear now is of new conflict among those players.

At a ceremony held later Saturday at the nearby al-Omar oil field base, a senior U.S. diplomat, William Roebuck, said the territorial defeat of the Islamic State group is a "critical milestone" that delivers a crushing and strategic blow to the extremist group. But he stressed it remains a significant threat.

"We still have much work to do to achieve an enduring defeat of IS," he said.

The commander in chief of the SDF, Gen. Mazloum Abdi, appealed for continued assistance to his group until the full eradication of the extremist group. He spoke at the ceremony during which fighters marched to a military band.

The victory declaration sets the stage for President Donald Trump to begin withdrawing most of the 2,000 U.S. troops stationed in northern Syria, as he abruptly announced in December that he would do. Trump, however, later agreed to leave a small peacekeeping force of 200 soldiers in Syria to ensure Turkey will not get into a conflict with the SDF. Turkey views Kurdish members of the SDF as terrorists.

The Kurds fear being abandoned by the Americans. They are squeezed between a belligerent Turkey from the north, which regards them as a national threat and Syrian President Bashar Assad's forces from the south.

Saturday's announcement came a day after Trump declared that Islamic State militants no longer control any territory in Syria, a victory he had been teasing for days.

Associated Press journalists in Baghouz on Saturday, however, reported hearing mortars and gunfire directed toward a cliff overlooking the village, where U.S.-led coalition airstrikes were carried out a day earlier. SDF spokesman Kino Gabriel said Friday there were ISIS fighters hiding in caves near Baghouz and that clearing operations were still underway.

The site of ISIS's last stand was centred on a tent encampment in Baghouz where, unknown to the besieging SDF forces, thousands of civilians were holed up. During the weeks-long siege, an estimate 30,000 men, women and children were evacuated from the pocket, most of them ISIS family members, a mix of Syrians, Iraqis and foreigners. They were exhausted, hungry, many of them wounded and traumatized by the loss of relatives, but some remained die-hard supporters of the "caliphate."

On Saturday, journalists were taken to the encampment -- a wasteland of wrecked vehicles, torn tents and scorched trees. A few bodies could be seen and a faint smell of rotting corpses hung in the air.

Scattered across the dirt amid empty foxholes and trenches were personal belongings, blankets, generators, oil barrels, water tanks and satellite dishes. Cars and motorcycles were turned to rusted, twisted heaps of metal. There were unused rockets, mortars and grenades, as well as a pile of suicide vests.

Ciya Kobani, an SDF commander, announced the end of the operation from the roof of the building with the SDF flag: "We have been victorious against Daesh," he declared.

At its height, the Islamic State group ruled a third of both Syria and Iraq, holding millions hostage to its harsh and violent interpretation of Islamic law. The group carried out massacres and documented them with slickly produced videos circulated online. It beheaded foreign journalists and aid workers and burned alive a captured Jordanian pilot. During a rampage through Iraq's Sinjar region in 2014, it captured thousands of women and girls from the Yazidi religious minority and forced them into sexual slavery. Many remain missing to this day.

The group also used its caliphate as a launchpad for attacks around the globe, including the assaults in Paris in 2015 that killed more than 130 people.

French President Emmanuel Macron tweeted that "a major danger to our country is now eliminated, yet the threat remains and the fight against terrorist groups must continue." France has been a member of the coalition fighting the ISIS since 2014.

While it imposed its unforgiving interpretation of Islamic law through public beheadings and crucifixions, the group also carried out the mundane duties of governance in its territories, including regulating prices at markets and repairing infrastructure.

Cornered in Baghouz, the group fought fiercely and desperately to hang on to the last shred of territory it controlled, using thousands of civilians, including women and children, as human shields. In the final weeks, they streamed out of Baghouz, bedraggled, angry and hungry, overwhelming Kurdish-run camps in northern Syria where they are being held.

Aid organizations say more than 100 people have died in the journey from Baghouz to the al-Hol camp in Hassakeh province, or soon after arriving.

------

Associated Press writer Zeina Karam in Beirut contributed.