MOSUL, Iraq — Dressed in a military uniform, Prime Minister Haider al-Abadi arrived here in Mosul on Sunday to congratulate Iraq’s armed forces on their victory over the Islamic State and mark the formal end of a bloody campaign that lasted nearly nine months, left much of Iraq’s second-largest city in ruins, killed thousands of people and displaced nearly a million more.

While there were reports that troops were still mopping up the last pockets of resistance and Iraqi forces could be facing suicide bombers and guerrilla attacks for weeks, the military began to savor its win in the shattered alleyways of the old city, where the Islamic State, also known as ISIS or ISIL, put up a fierce last stand.

Hanging over the declaration of victory is the reality of the hard road ahead. The security forces in Mosul still face dangers, including ISIS sleeper cells and suicide bombers. And they must clear houses rigged with explosive booby traps so civilians can return and services can be restored. Nor is the broader fight over: Other cities and towns in Iraq remain under the militants’ control

“It’s going to continue to be hard every day,” said Col. Pat Work, the commanding officer of the Second Brigade, 82nd Airborne Division, which is carrying out the American advisory effort here.

“Iraqi security forces need to be on the top of their game, and we need to be over their shoulder helping them as they move through this transition to consolidate gains and really sink their hold in on the west side,” Colonel Work said as he rolled through the streets of west Mosul recently in an armored vehicle. “ISIS will challenge this.”

The victory could have been sweeter, though, as the Iraqis were denied the symbolism of hanging the national flag from the Grand al-Nuri Mosque and its distinctive leaning minaret, which was wiped from the skyline in recent weeks as a final act of barbarity by Islamic State militants who packed it with explosives and brought it down as government troops approached.

It was at that mosque in June 2014 where Abu Bakr al-Baghdadi strode to the top of a pulpit and declared he was the leader of a caliphate straddling the borders of Iraq and Syria, a vast territory where for three years Islamist extremists have governed with a strict form of Islamic law, held women as sex slaves, carried out public beheadings and plotted terror attacks in the West.

This past week, as fighting raged nearby, Iraqi soldiers took selfies in front of the stump of the minaret and posed at the spot where Mr. Baghdadi made his speech. Destruction surrounded them, as did the stench of decaying bodies of Islamic State fighters, left to rot in the blazing sun.

The battle for Mosul began in October, after months of planning between Iraqis and American advisers, and some Obama administration officials had hoped it would conclude before they left office, giving a boost to the departing president’s efforts to defeat ISIS.

Instead, it lasted for nearly nine months, and was far more brutal than many expected. With dense house-to-house fighting and a ceaseless barrage of snipers and suicide bombers, the fight for Mosul was some of the toughest urban warfare since World War II, American commanders have said. Iraqi officers, whose lives have been defined by ceaseless war, said the fighting there was among the worst they have seen.

“I have been with the Iraqi Army for 40 years,” said Maj. Gen. Sami al-Aradi, a commander of Iraq’s special forces. “I have participated in all of the battles of Iraq, but I’ve never seen anything like the battle for the old city.” He continued: “We have been fighting for each meter. And when I say we have been fighting for each meter, I mean it literally.”

Victory came at great cost, with about a thousand deaths among the Iraqi security forces, many hundreds of civilians killed, some inadvertently by American airstrikes. At least seven journalists were killed, including two French correspondents and their fixer, an Iraqi Kurdish journalist, in a mine explosion in recent weeks.

The Iraqis and their international partners will now be confronted by the immense challenge of restoring essential services like electricity and rebuilding destroyed hospitals, schools, homes and bridges, which were wrecked in the ground combat or by the airstrikes and artillery and Himars rocket attacks carried out by the American-led coalition to help the Iraqi troops advance.

“When the fighting stops, the humanitarian crisis continues,” said Lise Grande, the deputy special representative for Iraq for the United Nations secretary general.

Western Mosul, especially its old city where the Islamic State made its last stand, was hit especially hard. As the combat has drawn to a close, thousands of civilians have begun to return. But 676,000 of those who left the western half of the city have yet to come back, according to United Nations data.

It is not hard to see why. Of the 54 neighborhoods in western Mosul, 15 neighborhoods that include 32,000 houses were heavily damaged, according to data provided by Ms. Grande. An additional 23 neighborhoods are considered to be moderately damaged. The cost of the near-term repairs and the more substantial reconstruction that is needed in Mosul has been estimated by United Nations experts at more than $700 million, she said.

Mosul was the largest city in either Iraq or Syria held by the Islamic State, and its loss signifies the waning territorial claims of a terrorist group that had its beginnings in the aftermath of the American invasion of Iraq in 2003. The group is also on the cusp of losing its de facto capital, the Syrian city of Raqqa, which is encircled by Arab and Kurdish fighters supported by the United States and backed by American firepower.

But the end of the Islamic State as a group holding territory does not mean peace is at hand, in Mosul or across Iraq. The group still holds the cities of Hawija and Tal Afar, in northern Iraq, and towns in the Euphrates River Valley in Iraq’s western Anbar Province. Iraqis expect an increase in terror attacks in urban centers, especially in the capital, Baghdad, as the group reverts to its insurgent roots.

Military victory has come without a political agreement between Iraq’s two largest communities, Sunni and Shiite Arabs, whose stark sectarian divisions led to the rise of ISIS in the first place. For many members of Iraq’s minority Sunnis, the Islamic State was seen as a protector against abuses they suffered under Iraq’s Shiite-led government, especially under the former prime minister, Nuri Kamal al-Maliki.

After the Islamic State seized Mosul in 2014, many Sunnis welcomed them. Mr. Maliki was then removed from office, replaced by Mr. Abadi, a more moderate and less-sectarian leader, but one widely viewed as weak. Under Mr. Abadi, there has been no meaningful reconciliation.

“I will leave Mosul because it has become a destroyed city,” said Aisha Abdullah, a teacher from Mosul who endured life under the Islamic State. “In every corner of it there is memory and blood.”

And while the Islamic State, with its harsh rule, alienated many of the Sunni residents it sought to represent, many residents said its ideology caught on among some of the population, especially young men.

“There is no use in reconstructing the city if the people of Mosul don’t change,” said Ms. Abdullah. “There are still many people who assist ISIS, and the acts of violence will never end.”

Marwan Saeed, another Mosul resident, who lives in the city’s east side, which was liberated in January and where life has largely been restored to normal, with schools and shops reopening and most civilians returning home, said he feared for the future, now more than ever.

“Frankly, I’m desperate over the future,” he said. “ISIS destroyed the people’s mentality, and the wars destroyed the infrastructure, and we paid the price. There is no such thing as the phase after ISIS. ISIS is a mentality, and this mentality will not end with guns alone.”

And there is the fear that many Islamic State fighters who were not captured or killed had simply put down their guns and blended in with the civilian population, to live to fight another day.

“Do you know that most of the ISIS fighters have shaved their beards and took off their clothes, and now they are free?” said Zuhair Hazim al-Jibouri, a member of Mosul’s local council.