Iraq's Prime Minister Haider al-Abadi has formally declared victory over the Islamic State of Iraq and the Levant (ISIL) in Mosul.

"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," Abadi said on Monday evening, using an Arabic acronym for Islamic State.

Abadi, wearing a black military uniform and flanked by commanders from the security forces, thanked troops and the US-led coalition that backed the offensive. 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.

A 100,000-strong coalition of Iraqi government units, Kurdish Peshmerga fighters and Shia armed groups launched the offensive to recapture the city from the militants in October, with key air and ground support from an international coalition.

The commander of the US-led coalition said Iraqi troops have firm control of the city, but some areas still must be cleared of explosive devices and possible ISIL fighters.

"This victory alone does not eliminate [ISIL] 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," Lieutenant-General Stephen Townsend said in a statement.

'Lost everything'

There were huge celebrations in cities across Iraq on Monday evening after Abadi's declaration of victory.

People gathered in the capital Baghdad's Tahrir Square and in Basra, dancing and waving flags as others took to the streets in convoys of motorcycles and cars.

Ali Mohsen, a Baghdad resident, praised Iraqi troops and said: "We hope to uproot Daesh so the displaced can return to their homes."

A spokesman for UN Secretary-General Antonio Guterres said on Monday the recovery of Mosul was "a significant step in the fight against terrorism and violent extremism".

It was in Mosul three years ago that ISIL leader Abu Bakr al-Baghdadi declared the caliphate and himself the leader of the world's Muslims.

The cost of the nearly nine-month battle to retake the city has been enormous: much of Mosul in ruins, thousands dead and wounded and nearly a million people forced from their homes.

OPINION: What's next after Mosul?

Civilians who have fled fighting in Mosul face a dire humanitarian crisis. The Norwegian Refugee Council (NRC) say displaced civilians are living in "incredible hardship" in temporary camps.

"Temperatures in this part of Iraq are topping 45 degrees. People are living in camps. They need a lot of water, and the threat of disease looms in this kind of weather," the NRC's Melany Markham told Al Jazeera from Erbil.

The UN says 920,000 civilians have fled their homes since the military campaign began in October. Close to 700,000 people are still displaced.

The UN refugee agency said on Monday it could be many months before civilians are able to return to their homes because of "extensive damage caused during the conflict".

"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," Lise Grande, the UN's humanitarian coordinator in Iraq, said in a statement.

"Many of the people who have fled have lost everything. They need shelter, food, healthcare, 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."

While defeat in Iraq's second-largest city deals a heavy blow to ISIL, the group controls several cities and towns south and west of Mosul.

ISIL is also under heavy pressure in its operational headquarters in the Syrian city of Raqqa and its self-proclaimed caliphate that once straddled the two countries is crumbling.