Iraq has declared “total victory” over the Islamic State group in Mosul, retaking full control of the country's second-largest city three years after it was seized by extremists bent on building a global caliphate.

“This great feast day crowned the victories of the fighters and the Iraqis for the past three years,” said Prime Minister Haider al-Abadi late last night, flanked by his senior military leadership at a small base on the edge of the Old City, where the final battles for Mosul unfolded.

Al-Abadi alluded to the brutality of the battle for Mosul    Iraq's longest yet in the fight against IS, saying the triumph had been achieved “by the blood of our martyrs.”

While Mosul fell to IS in a matter of days in 2014, the campaign to retake the city lasted nearly nine months. The fight, closely backed by airstrikes from the U.S.-led coalition, brought an end to the extremists' so-called territorial caliphate,

but has also left thousands dead, entire neighbourhoods in ruins and nearly 900,000 displaced from their homes.

Shortly after al-Abadi's speech, the coalition congratulated him on the victory but noted that parts of the Old City still “must be back-cleared of explosive devices and possible ISIS fighters in hiding.” ISIS, ISIL and Daesh are alternative acronyms for the Islamic State group.

“The victory in Mosul, a city where ISIS once proclaimed its so-called 'caliphate,' signals that its days in Iraq and Syria are numbered,” President Donald Trump said in a statement.

Earlier in the day, airstrikes pounded the last IS-held territory on the western edge of the Tigris, Humvees rushed wounded to field hospitals and soldiers hurriedly filled bags with hand grenades to ferry to the front.

Iraqi troops had slowly pushed through the narrow alleys of the Old City during the past week, punching holes through walls and demolishing houses to carve supply routes and fighting positions in a district where many of the buildings date back centuries.

For days, the remaining few hundred militants held an area measuring less than a square kilometer (less than half a square mile), and Iraqi commanders described victory as imminent.

Al-Abadi also visited Mosul on Sunday, congratulating the troops on recent gains but stopping short of declaring an outright victory as clashes continued.

The drawn-out endgame in Iraq's fight for Mosul highlighted the resilience of the extremists and the continued reliance of Iraqi forces on air support to retake territory.

Iraqi commanders said gains slowed to a crawl in recent days as IS fighters used their families    including women and children as human shields. As the battle space constricted, the coalition began approving airstrikes dropping bombs of 200 pounds or more on IS targets within 50 meters (yards) of friendly forces.

Plumes of smoke Monday grew larger than the strip of territory under IS control. “This used to be a beautiful city, tourists used to come here,” said Iraqi army Capt. Marwan Hadi based inside the Old City. The last days of the fight for Mosul were the fiercest, he said.

“All along the front line, there are so many families under the rubble,” he said. “I saved two children and their mother, but one daughter, we couldn't reach her.”

Reports of civilian casualties spiked as Iraqi forces punched into Mosul's western half in February. Residents fleeing the fighting reported that entire families sheltering in the basements of their homes were killed by airstrikes targeting small teams of IS fighters.

Thousands of civilians were estimated to have been killed in the fight for Mosul, according to Nineveh's provincial council. A toll that does not include those still believed buried under collapsed buildings.