Iraq declared victory against the Islamic State group in Mosul on Sunday after a grueling months-long campaign, dealing the biggest defeat yet to the jihadist group.
Prime Minister Haider al-Abadi's office said he was in "liberated" Mosul to congratulate "the heroic fighters and the Iraqi people on the achievement of the major victory," three years after ISIS declared its self-styled caliphate from the city.
The fighting did not seem to be completely over, with gunfire and explosions still audible in the city, but Abadi's arrival had been expected for days as a signal of the formal end of the battle for Mosul.
The victory comes at an enormous cost: much of Iraq's second city in ruins, thousands dead and wounded, and nearly a million people forced from their homes.
And enormous challenges lie ahead, not just in rebuilding Mosul but in tackling the continued presence elsewhere of ISIS, which remains a potent force.
Photographs released by his office showed Abadi dressed in a black military uniform and cap, shaking hands with police and army officers.
His office said Abadi held meetings with commanders in Mosul and issued a series of commands on "sustaining victories and eliminating the defeated remnants" of ISIS, as well as "establishing security and stability in the liberated city."
Iraqi forces celebrated, waving flags and flashing victory signs, after Abadi arrived in the city.
"This victory is for all Iraqis, not just for us," Mohanned Jassem, a member of the elite Counter-Terrorism Service, told AFP at the police base where Abadi met commanders.
Jassem, who fought in most of the other main battles of the war against ISIS, said Mosul was the toughest.
"I took part in fighting in Ramadi and Tikrit and Salaheddin and Baiji and Al-Qayara... but the fighting here in [ISIS's] stronghold was the most violent," he said, an Iraqi flag draped over his shoulders.
ISIS swept across much of Iraq's Sunni Arab heartland in a lightning offensive in mid-2014, proclaiming a "caliphate" straddling Iraq and neighboring Syria.
Imposing its strict interpretation of Islamic law, the group committed widespread atrocities and organized or inspired deadly attacks in Iraq, Syria and abroad.
A US-led coalition launched military operations against ISIS in Syria and Iraq in mid-2014, carrying out waves of air strikes against the jihadists and sending advisers to work with local ground forces.
French President Emmanuel Macron, whose country is a key part of the coalition, was among the first world leaders to offer his congratulations.
"Mosul liberated from Daesh," he tweeted, using an Arabic acronym for IS. "Homage from France to all those, with our troops, who contributed to this victory."
ISIS has lost most of the territory it once controlled and after Mosul the coalition is aiming to oust the jihadists from their Syrian stronghold Raqa, which is under assault by US-backed Arab and Kurdish forces.
Iraqi forces launched their campaign to recapture Mosul in October, seizing its eastern side in January and launching the battle for its western part the next month.
But the fight grew tougher when Iraqi forces entered the densely populated Old City on the western bank of the Tigris River that divides the city.
Related: Iraq Interrupted, an in-depth series on stories of survival
In recent days, security forces have killed jihadists trying to escape their dwindling foothold in Mosul, as Iraqi units fought to retake the last two IS-held areas near the Tigris.
Earlier Sunday Iraq's Joint Operations Command had said it killed "30 terrorists" trying to escape across the river.
Even in the final days of the battle, thousands of civilians remained trapped inside the Old City and those who fled arrived grief-stricken after losing relatives in jihadist sniper fire and bombardments.
The United Nations said this week that since October around 915,000 residents had fled Mosul, which had a population of two million three years ago.
The recapture of Mosul will not mark the end of the threat posed by IS, which holds territory elsewhere in Iraq and is able to carry out frequent bombings in government-held areas.
In Iraq it holds towns including Tal Afar and Hawijah in the north, as well as a stretch of territory in western Anbar province.
It also continues to hold significant territory in Syria including Raqa, where the US-backed Syrian Democratic Forces (SDF) are battling to oust the jihadist group after penetrating its fortified historic centre.
Analysts warned that while the loss of Mosul was a major blow to the jihadists it was not yet a fatal one.
"We should not view the recapture of Mosul as the death knell for IS," said Patrick Martin, Iraq analyst at the Institute for the Study of War.
"If security forces do not take steps to ensure that gains against IS are sustained for the long-term, then IS could theoretically resurge and recapture urban terrain," he said.
By AFP's Emmanuel Duparcq in Mosul, with W.G. Dunlop in Baghdad.
Sign up for The Top of the World, delivered to your inbox every weekday morning.