Philippine President Rodrigo Duterte made an unannounced visit to Marawi on Tuesday (17 October) and declared the city has been "liberated from Islamist militants". However, journalists could hear the sporadic gunfire and explosions of ongoing fighting about a kilometre (half a mile) away from the school campus where Duterte delivered his rousing address to cheering soldiers.

Photos taken on Wednesday 18 October – the day after the city was "liberated" – show clouds of smoke and tear gas billowing above the city.

The military explained that Duterte's declaration was symbolic. They said there are 20 to 30 heavily-armed militants, including six to eight foreign fighters, still holed-up in an area of the city covering about two hectares (five acres). They are thought to have about 20 hostages, including women and children.

Military chief General Eduardo Ano told The Associated Press that Duterte meant the threat from the militants, who have occupied parts of the lakeside city for five months, is substantially over. "They're leaderless and they have no more organisation," he said.

The last two surviving leaders of the siege were killed during a targeted military operation on Monday (17 October). Troops were finally able to pinpoint the location of the elusive Isnilon Hapilon, who was listed among the FBI's most-wanted terror suspects, and Omarkhayam Maute thanks to information provided by a hostage rescued two days earlier.

Another leader, Abdullah Maute, has yet to be accounted for. Intelligence indicated he died in an August air strike, though no body was found. Malaysian militant Mahmud bin Ahmad, who uses the nom de guerre Abu Handzalah and is a close associate of Hapilon, has also not been found and is among the remaining militants being hunted by troops.

The 148-day occupation triggered concerns that the island of Mindanao could become a magnet for Isis fighters driven out of Iraq and Syria. Many security experts believe Abu Handzalah, a recruiter and fundraiser who trained at an al-Qaeda camp in Afghanistan, could replace Hapilon as Islamic State's de facto leader in Southeast Asia.

When Isis-backed militants took over parts of the picturesque lakeside city of Marawi in May, the Philippines's defence minister Delfin Lorenzana predicted the entire conflict would be over in one week. However, the militants surprised security forces with their combat prowess, the volume of arms and ammunition they stockpiled and their ability to withstand intensive air strikes aided by US surveillance drones and technical support. The military repeatedly missed targets and deadlines to crush the rebels.

Borrowing heavily from Isis tactics in the Iraqi city of Mosul, the militants surrounded themselves with hostages and used snipers and a network of tunnels. Marawi's underground drainage system and "rat holes" – crevices in the walls of high floors allowing access to adjacent buildings – enabled the rebels to evade bombs and remain undetected, soldiers at the battlefront said.

More than 1,000 people, mostly rebels, have been killed since the fighting began, and the heart of the city of 200,000 people has been levelled by air strikes. Streets are lined with pockmarked buildings and houses, their roofs either blasted away or riddled with bullet holes. Duterte apologised to the people of Marawi for the destruction. "We had to do it," he said. "There was no alternative."

Defence Secretary Delfin Lorenzana, who estimated Marawi operations to have cost five billion pesos (about £75m), said reconstruction could start in January. "There are still stragglers and the structures are still unsafe because of unexploded ordnance and improvised explosive devices," he said on radio.