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."
But Mr Abadi warned that more challenges lay ahead.
"We have another mission ahead of us - to create stability, to build and clear Daesh cells.
"And that requires an intelligence and security effort, and the unity which enabled us to fight Daesh," he added, before waving an Iraqi national flag.
The US-led coalition confirmed that while areas of the Old City still had to be cleared of explosive devices and possible IS fighters in hiding, the Iraqi security forces had Mosul "firmly under their control".
The senior American commander in Iraq, Lt Gen Stephen Townsend, congratulated the Iraqi security forces on their "historic victory against a brutal and evil enemy".
"Make no mistake; this victory alone does not eliminate [IS] and there is still a tough fight ahead. But the loss of one of its twin capitals and a jewel of their so-called caliphate is a decisive blow," he said.
Mr Abadi had to delay declaring the "liberation" of Mosul on Sunday because a pocket of resistance remained in the Old City, on the western bank of the River Tigris, that 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 were heard coming from the area earlier on Monday.
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."
- Where is 'Islamic State' leader Abu Bakr al-Baghdadi?
- After Mosul: The Iraqi towns still under IS control
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.
It 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.