"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," Haider al-Abadi said in a speech shown on state television, using an Arabic acronym for Islamic State.
A 100,000-strong coalition of Iraqi government units, Kurdish Peshmerga fighters and Shi'ite militias launched the offensive to recapture the city from the militants in October, with key air and ground support from a US-led coalition.
Abadi, wearing a black military uniform and flanked by commanders from the security forces, thanked troops and the coalition. But he 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 said before raising an Iraqi flag.
Abadi arrived in Mosul on Sunday to congratulate military commanders who have waged a nearly nine-month battle to recapture the city, many parts of which were reduced to rubble.
Gunfire and explosions could be heard earlier in the day as the last few Islamic State positions were pounded.
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.
Since the 2003 US-led invasion that toppled Saddam Hussein, the Shi'ites have been politically dominant in Iraq but the country has been racked by ethnic conflict.
In the aftermath of victory in Mosul, Abadi's government faces the task of managing the sectarian tensions there 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 UN 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.
Islamic State is also under heavy pressure in its operational headquarters in the Syrian city of Raqqa.
The stench of corpses along Mosul's streets was a reminder of the gruelling urban warfare required to dislodge Islamic State.
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.