BEIRUT -- The Islamic State of Iraq and Syria (ISIS), responsible for some of the worst atrocities perpetrated against civilians in recent history, is on the verge of collapse.

After brutalizing residents living under its command for more than three years, the militants have now lost their self-proclaimed capital of Raqqa and are battling to hang on to relatively small pockets of territory in Iraq and Syria, besieged by local forces from all sides. Few, however, expect ISIS to completely go away, or for the bloodshed in the two countries and the region to end quickly.

Here's a look at the rise and fall of the Sunni Muslim extremist group's "caliphate," and what to expect next:

Get Breaking News Delivered to Your Inbox

A "caliphate" no more

ISIS, which emerged from the remnants of al Qaeda in Iraq, began its spread across the Mideast in early 2014, overrunning the Iraqi city of Fallujah and parts of the nearby provincial capital of Ramadi. In Syria, it seized sole control of the city of Raqqa after driving out rival Syrian rebel factions.

In June 2014, ISIS captured Mosul, Iraq's second-largest city, from where its leader, Abu Bakr al-Baghdadi, declared a self-styled "caliphate," a declaration tantamount to an earthquake that would temporarily redraw borders and shake up the entire region.

ISIS promised justice, equality and an Islamic, religious utopia. But over the next few years, it terrorized people living under its control, systematically slaughtering members of Iraq's tiny Yazidi community, kidnapping women and girls as sex slaves, beheading Western journalists and aid workers and destroying some of the Mideast's spectacular archaeological and cultural sites.

CBS News' Holly Williams, one of the only Western television correspondents to make it into Raqqa this week, said Tuesday that it was surreal to report from the center of the city, where just weeks earlier she would almost certainly have been kidnapped and executed.

ISIS also attracted a motley crew of foreign fighters, mostly marginalized European youths and other foreigners who took up its cause. But it alienated mainstream Sunni Muslims, who found ISIS' crude interpretation of Islam also spreading in areas far from Syria and Iraq.

Creating a territorial caliphate created a target, and an international anti-ISIS coalition soon took shape.

What's left

The United States launched its campaign of airstrikes against ISIS in Iraq in August 2014, and a month later in Syria. In Iraq, it partnered with government forces working with state-sanctioned Shiite-led militias as well as Iraqi Kurdish fighters known as peshmerga. In Syria, it partnered with local Syrian Kurdish-led fighters, the Syrian Democratic Forces.

Supported by tens of thousands of U.S.-led airstrikes, these forces drove ISIS militants from one stronghold after another over the years. The biggest blow came in July when Mosul, long regarded as ISIS' administrative capital, was liberated.

In Syria, IS appears to be heading for collapse as the U.S.-backed, Kurdish-led Syrian Democratic Forces, or SDF, and Syrian government forces, backed by their Russian allies, are attacking them in separate, simultaneous offensives.

A senior SDF commander on Tuesday said his forces liberated Raqqa from IS militants and would formally announce victory soon after clearing operations to remove land mines and search for sleeper cells. Mayadeen, a town in the heart of Syria's Euphrates River Valley near Iraq's border where the militants had been expected to make their last stand, fell to Syrian government troops over the weekend.

In northern Iraq, the jihadis no longer hold any cities or towns after their stronghold of Hawija fell earlier this month. Iraq's army is now gearing up to fight ISIS in its last territory -- the sprawling desert Anbar province stretching all the way to the Syrian border. In Syria, ISIS still holds the town of Boukamal near the Iraqi border and scattered pockets of territory in the east.

Staggering price

The destruction of ISIS has come at a devastating cost for both Syria and Iraq, and immense suffering for those who endured the militants' brutal reign.

The fighting and airstrikes have pulverized once thriving cities, turning them into tragic vistas of crushed apartment blocks, flattened homes and collapsed roads and bridges. In Ramadi, Mosul and Raqqa, the scope of the damage is staggering.

Williams reported Tuesday that most, if not all of the residents of Raqqa have now fled. When they return, they will face a daunting task rebuilding what was once one of Syria's biggest cities.

Two weeks ago, the U.S.-led coalition announced it has returned more than 83 percent of ISIS-held land to local populations since 2014, liberating more than 6 million Syrians and Iraqis in the process. At least 735 civilians have been unintentionally killed by coalition strikes, although activists and war monitors estimate the toll to be much higher.

The nine-month battle to liberate Mosul resulted in the death of up to 1,500 Iraqi forces. At least 1,100 SDF fighters were killed in the battles for Syria's Raqqa and Deir el-Zour up until late September, according to the coalition.

In the three years since ISIS began building its "caliphate," it has killed thousands of people, displaced millions and worked hard on infusing children with extremist doctrine.

Shifting of sands

The rise of ISIS and subsequent wars and alliances to bring about its defeat has worsened political and sectarian fault-lines in Syria and Iraq.

It gave unprecedented clout to Kurdish populations in both countries, unsettling their central governments, as well as Iran and Turkey, both battling Kurdish separatists within their own borders.

Under cover of the fight against ISIS, Iraq's Kurds seized the oil-rich city of Kirkuk in 2014 -- a move Baghdad has now reversed, moving into the city, seizing oil fields and other infrastructure in an attempt to curb Kurdish aspirations for independence.

The shifting and chaotic battlefields in Syria's civil war, tensions between Kurds and ethnic Arabs, the presence of Shiite militias and government troops in predominantly Sunni towns and cities vacated by ISIS may lead to more violence.

In many ways, the fight over ISIS spoils and territories is only just beginning.

Uncertain future

All forces battling ISIS will have to remain vigilant even after they recapture the last militant-held territory. In some ways, they now face an even more daunting challenge.

Hisham al-Hashem, an Iraqi writer and analyst, estimates there remain 8,000 jihadis in Iraq's Anbar who will melt away "like salt in water" to wait for the right moment to launch their next insurgency or suicide attack.

ISIS affiliates continue to carry out attacks in Egypt and Libya, where the group gained a foothold and which could be its preferred theaters for retaliation. Before it broke away from al Qaeda and rebranded itself as the "Islamic State," al Qaeda in Iraq waged a years-long insurgency following the 2003 U.S. invasion of Iraq, pushing the country to the brink of civil war.