(Sarah Parnass,Jesse Mesner-Hage/The Washington Post)

Iraq’s prime minister entered the city of Mosul on Sunday to declare victory in the nine-month battle for control of the Islamic State’s former stronghold, signaling the near end of the most grueling campaign against the group to date and dealing a near-fatal blow to the survival of its self-declared caliphate.

On a walk through the city’s eastern districts, Haider al-Abadi was thronged by men holding cameraphones as music blared and others danced in the streets.

“The world did not imagine that Iraqis could eliminate Daesh,” he said, using the Arabic acronym for the Islamic State. “This is all a result of the sacrifices of the heroic fighters who impressed the world with their courage.” 

But in a sign of how tenaciously the Islamic State has fought, even as Abadi was touring the town, the sound of airstrikes echoed through the skies and smoke rose from the last pocket of territory the militants control, thought to be no more than 200 yards long and 50 yards wide.

The confusion of that moment came as a reminder that even though a complete victory now seems assured, it has come at a tremendous price. On a walk through Mosul’s oldest quarters on Sunday, the stench of bodies filled the air. Between the rubble and rebar were the arms of a young child, still wrapped in pale pink sleeves.

As he toured the city, Abadi met commanders in west Mosul who led the battle but did not make a formal speech declaring the city free of militants, though one had been expected.

The battle has been the toughest yet in the Islamic State war, and one that lasted far longer than anticipated. When the offensive was launched in October, U.S. officials were privately predicting a two-month fight, expressing hope that mass civilian displacement and widespread destruction could be avoided.

Instead, the fight lasted for nine months, longer than the siege of Stalingrad and longer than the final Allied push into Germany in World War II. It has cost thousands of lives, uprooted hundreds of thousands of people and shattered vast stretches of the city.

And the declaration of victory does not end the war. The Islamic State cannot now roll back the array of forces ranged against it. It is on a path to defeat in the Syrian city of Raqqa, the original capital of the militants’ so-called state, where an offensive launched by U.S.-backed Kurdish and Arab forces is making progress. But that battle is just getting started.

Over the past three years since the Islamic State leader Abu Bakr al-Baghdadi declared the existence of a “caliphate” in Mosul, his group has been driven out of 60 percent of the territory it once controlled in both Iraq and Syria, according to the U.S. military.

But that still leaves it in control of an extensive chunk of land spanning the border of the two countries and several other pockets, including key towns such as Hawija, Tal Afar and Qaim in Iraq and most of the entire province of Deir al-Zour in Syria.

As the battle for Mosul has demonstrated, the Islamic State is prepared to fight for every inch it holds, even as the neighborhoods its cadres lived in are destroyed around them. U.S. officials won’t put a timeline on how much longer the war will last, but most analysts predict it will continue throughout this year and perhaps much of 2018.

And even after that there is the question of how and when the defeated militants will seek to regroup in the shadows of the ruined cities they have lost, to wage the kind of insurgency that fueled their rise in the decade before their conquests.

“Talk about complete military defeat is one thing. What ISIS devolves into is another discussion. Will they revert back into a terrorist organization?” said Col. Ryan Dillon, the U.S. military spokesman in Baghdad, using another acronym for the Islamic State.

“The loss of Mosul means ISIS is no longer the same, for better or worse. It’s no longer the quasi-state that it projected itself to be. But everything achieved against the group is fragile. The ideology is still there, the appeal is still there, and so are the divisions that helped them take power,” said Hassan Hassan, a resident fellow at the Tahrir Institute for Middle East Policy.

There is also the question of rebuilding Mosul. Many of the hundreds of thousands of people who fled the fighting to refugee camps nearby will find their homes destroyed. The scale of the misery is vast, and far from being adequately addressed.

Thousands of civilians had poured out of the Islamic State’s shrinking redoubt in recent weeks, many of them in tears as they stumbled to safety. Stuck between the militants and the U.S.-led coalition airstrikes propelling the campaign to save them, many said they had spent weeks with barely any food or water. Without medical care, the wounded had died in or under their homes.

Mosul was the largest city to fall to Islamic State control. Three years after the caliphate was declared here at a medieval mosque, that building lies in ruins, after the Islamist militants blew it up as Iraqi forces moved in. 

The United Nations predicts that at least $1 billion will be required to rebuild Mosul’s basic infrastructure. More extensive reconstruction could cost billions more.

In parts of western Mosul, streets have been leveled. Debris and twisted metal are piled high through the alleyways, burying mattresses, flip-flops and other remnants of the lives Islamic State fighters built there. No one here knows how many civilians also remain under the rubble of their homes.

In the final days of the battle, commanders said militants had sent suicide bombers out among fleeing civilians and used children as human shields in the winding alleyways of the Old City.

Standing amid the ruins, Staff Sgt. Rasoul Saeed said the fight had been “incomparable.” 

“It is the hardest battle we have ever fought. At the end we are bogged down in alleyways, without vehicles, alone against the enemy,” he said.

In Mosul’s eastern districts, the first to be recaptured from the Islamic State, a relative lack of damage has seen life return to some kind of normalcy. The sidewalks were bustling Sunday night with fast food shops running a roaring trade.

But residents said the legacy of three years of Islamic State rule would be hard to forget. “They tortured me in their prison without mercy because I once served as a police officer,” said Karam Abu Taif, his voice wavering on the verge of tears.

“Everyone here has a story now,” he said. “I cannot forget. We will not forget.”

Sly contributed to this report from Beirut.