Iraq's prime minister declared victory over Islamic State in Mosul on Monday, three years after the militants seized the city and made it the stronghold of a "caliphate" they said would take over the world.
Haider al-Abadi made the announcement from the operations room of the Counter-Terrorism Service, his media office said on Twitter.
He arrived in Mosul on Sunday to congratulate military commanders who have waged a nearly nine-month battle to recapture the city from Islamic State.
Gunfire and explosions could be heard earlier in the day as the last few Islamic State positions were pounded.
Mr. Abadi has been meeting military and political officials in Mosul in an atmosphere of celebration that contrasts with the fear that spread after a few hundred Islamic State militants seized the city and the Iraqi army crumbled in July 2014.
Islamic State leader Abu Bakr al-Baghdadi shocked the Middle East and Western powers shortly afterwards by appearing at the pulpit of Mosul's Grand al-Nuri Mosque to declare the caliphate and himself the leader of the world's Muslims.
A reign of terror followed which eventually alienated even many of those Sunni Muslims who had supported the group as allies against Iraq's Shi'ite majority.
In the aftermath of victory, Abadi's government now faces the task of managing the sectarian tensions in Mosul and elsewhere that enabled Islamic State to win support, and the threat of a wave of revenge violence in the city.
Baghdadi has fled the city and his whereabouts are unknown. Reports have circulated that he is dead but Iraqi and Western officials say they cannot corroborate this.
His death or capture would not be the end of Islamic State, which still controls areas south and west of Mosul and which is now expected to take to the desert or mountains to wage an insurgency.
The militants are expected to keep trying to launch attacks on the West and inspiring violence by "lone wolves" or small groups of the kind mounted recently in Britain, France, and elsewhere.
But the loss of Iraq's second-largest city is a grave body blow to Islamic State.
"The recovery of Mosul is a significant step in the fight against terrorism and violent extremism," said the spokesman for United Nations Secretary-General Antonio Guterres.
Jaafar Sadiq, a member of Iraq's counter-terrorism force, said military operations had been completed in Mosul's Old City, which saw heavy fighting in recent weeks as the Islamists made their last stand.
The Islamic State is also under heavy pressure in its operational headquarters in the Syrian city of Raqqa.
Much of the city of 1.5 million has been destroyed in the fighting, its centuries-old stone buildings flattened by air strikes and other explosions. One of Islamic State's last acts was to blow up the historic Al Nuri mosque and its famous leaning minaret.
Thousands of people have been killed. The United Nations says 920,000 civilians have fled their homes since the military campaign began in October. Close to 700,000 people are still displaced.
"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 UN Humanitarian Coordinator for 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."
Iraqi soldiers relaxed. Some swam in the Tigris river which runs through the city. One wiped the sweat from his face with an Islamic State flag.