MOSUL, Iraq — Dressed in a military uniform, Prime Minister Haider al-Abadi arrived in Mosul on Sunday to congratulate Iraq’s armed forces for wresting the city from the Islamic State.

The victory marked 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 1 million more.

While Iraqi troops were still mopping up the last pockets of resistance and Iraqi forces could be facing guerrilla attacks for weeks, the military began to savor its win in the shattered alleyways of the old city, where the Islamic State put up a fierce last stand.

Hanging over the declaration of victory is the reality of the hard road ahead.

Get Today’s Headlines in your inbox:
The day’s top stories delivered every morning.

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 Colonel Pat Work, commanding officer of the 2nd Brigade, 82nd Airborne Division, which is carrying out the US 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,” Work said. “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 Islamic extremists have governed with a strict form of Islamic law, held women as sex slaves, carried out public beheadings, and plotted terror attacks against 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 al-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 US 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 until now, 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, U.S. commanders have said.

Even as al-Abadi arrived here outfitted in the black uniform of Iraq’s elite Counterterrorism Service, Iraqi forces were pressing to erase a pocket of Islamic State resistance by the Tigris River.

Speaking from his base in the old city, Lieutenant General Abdul-Wahab al-Saadi, a senior commander in that service, said the militants’ enclave was about 200 yards long and 50 yards wide and that he expected it to be taken later in the day or on Monday

Deaths among Iraqi security forces in the Mosul battle had reached 774 by the end of March, according to US officers, and toll is believed to be more than 1,000 now.

Even more civilians are estimated to have been killed, many at the hands of the Islamic State and some inadvertently by US 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 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 rocket attacks carried out by the US-led coalition.

Western Mosul, especially its old city where the Islamic State made its last stand, was hit especially hard and is now a gray and decimated landscape. 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 UN data. Mosul was the largest city in either Iraq or Syria held by the Islamic State, and its loss signifies the waning territorial claims of the terrorist group.

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 US firepower.