(BBC) -- Iraqi Prime Minister Haider al-Abadi has formally declared victory over so-called Islamic State (IS) in Mosul.
Mr Abadi waved a national flag with troops after announcing the "collapse of the terrorist state of falsehood".
Earlier, clashes were reported in a small part of Old City where a few dozen IS militants were holding out.
The battle for Mosul has taken almost nine months, left large areas in ruins, killed thousands of civilians and displaced more than 920,000 others.
Commanders from the US-led coalition that has provided air and ground support to Iraqi forces said the urban combat had been most intense since World War Two.
Mr Abadi made the declaration of victory at the operations room of the Counter-Terrorism Service, whose elite forces were the first to enter Mosul in November.
Using a pejorative term for IS based on an Arabic acronym of its former name, he said: "I announce from here the end and the failure and the collapse of the terrorist state of falsehood and terrorism, which the terrorist Daesh announced from Mosul."
Photographs posted by the prime minister's office showed Mr Abadi waving an Iraqi national flag, surrounded by cheering troops and armoured Humvees.
Mr Abadi arrived in Mosul on Sunday, apparently expecting to congratulate his "heroic fighting forces" on "liberating" the city.
However, he had to delay making a formal declaration because a pocket of resistance remained in the Old City, on the western bank of the River Tigris, that one commander said was only about 180m (200 yards) long and 45m wide.
Iraqi army officers believed only a few dozen militants were left inside along with their wives and children, who they said were being used as human shields.
Gunfire and explosions could be heard as US-led coalition aircraft bombed the area.
Mosul has been devastated by the battle, with the UN estimating that more than 5,000 buildings have been damaged and 490 destroyed in the Old City alone.
"It's a relief to know that the military campaign in Mosul is ending. The fighting may be over, but the humanitarian crisis is not," said the UN Humanitarian Coordinator in Iraq, Lise Grande.
"Many of the people who have fled have lost everything. They need shelter, food, health care, water, sanitation and emergency kits. The levels of trauma we are seeing are some of the highest anywhere. What people have experienced is nearly unimaginable."
IS militants overran Mosul in June 2014, before seizing control of large parts of northern and western Iraq. The following month, Abu Bakr al-Baghdadi made his first and only public appearance as IS leader at the city's Great Mosque of al-Nuri, and gave a speech proclaiming the creation of a "caliphate".
IS blew up the mosque almost exactly three years later as Iraqi troops prepared to retake it - a move Iraq's prime minister called a "formal declaration of defeat".
The BBC's Jonathan Beale in Mosul says the jihadists may have lost militarily in the city, but this is not their end in Iraq nor the end of their twisted ideology.
IS still controls territory in three areas of Iraq - around Hawija, 130km (80 miles) south-east of Mosul; around Tal Afar, 65km west; and from Ana to Al-Qaim in the Euphrates river valley, 250km to the south-west.
IS also holds a string of towns along the Euphrates in Syria, including Albu Kamal and Mayadin, but its stronghold of Raqqa is besieged by US-backed fighters.