BEIRUT — U.S.-backed forces celebrated in the devastated streets of Raqqa on Tuesday after gaining control of the northern city that once was the heart of the Islamic State’s self-styled caliphate, dealing a major defeat to the extremist group that has seen its territory shrink ever smaller since summer.

Militants took over the vibrant metropolis on the Euphrates River in 2014, transforming it into the epicenter of their brutal rule, where opponents were beheaded and terror plots hatched.

It took thousands of bombs dropped by the U.S.-led coalition and more than four months of grueling house-to-house battles for the Syrian Democratic Forces to recapture Raqqa, marking a new chapter in the fight against the group whose once vast territory has been reduced to a handful of towns in Syria and Iraq. “Liberating Raqqa is a triumph for humanity, especially women,” who suffered the most under IS, said Ilham Ahmed, a senior member of the SDF.

The city’s infamous central square was the site of public beheadings and other killings by the militants. Bodies and severed heads would be displayed there for days, mounted on posts and labeled with their alleged crimes, according to residents who later dubbed it “Hell Square.” Crumbled and flattened buildings stood behind the celebrating fighters as they drove around the square, a sign of the massive destruction the city has suffered since the militants took over. It was in Naim Square that the extremists paraded tanks and military hardware in 2014 in a chilling show of force that was a sign of things to come.