Philippine president Rodrigo Duterte declared the southern city of Marawi liberated from pro-ISIL militants on Tuesday, although the military said 20-30 rebels were holding about 20 hostages and still fighting it out.
In a rousing address to soldiers a day after the killing of two commanders of the rebel alliance, Mr Duterte said he would never again allow militants to stockpile so many weapons, but Marawi was now free and it was time to heal wounds and rebuild.
"I hereby declare Marawi City liberated from terrorist influence, that marks the beginning of rehabilitation," Mr Duterte, wearing a camouflage cap and dark sunglasses, said during his unannounced visit.
Isnilon Hapilon, who was wanted by the United States and was ISIL's Southeast Asian "emir", and Omarkhayam Maute, one of two brothers central to the alliance, were killed in a targeted operation on Monday. Their bodies were recovered and identified, authorities said.
The 148-day occupation marked the Roman Catholic-majority Philippines' biggest security crisis in years and triggered concerns that with its mountains, jungles and porous borders, the island of Mindanao could become a magnet for ISIL fighters driven out of Iraq and Syria.
More than 1,000 people, mostly rebels, were killed in the battle and the heart of the city of 200,000 has been levelled by air strikes.
Armed forces chief Eduardo Ano said the remaining gunmen were now a "law enforcement matter", while military spokesman Restituto Padilla described them as "stragglers".
"There is no way that they can get out anymore, there is no way for anyone to get in," Mr Padilla told news channel ANC.
Mr Padilla said the military believed Malaysian operative Mahmud Ahmad was in Marawi, but it could not be certain. He said Mahmud was no threat.
"Dr Mahmud is an academic, he's not a fighter," Mr Padilla said. "We don't feel he is a problem."
But some security experts say otherwise and believe Mahmud, 39, a recruiter and fundraiser who trained at an Al Qaeda camp in Afghanistan, could replace Hapilon as ISIL's point-man in Southeast Asia.
Another leader, Abdullah Maute, has yet to be accounted for. Intelligence indicated he died in an August air strike, though no body was found.
Defence officials say the core leadership was key to recruiting young fighters and arranging for extremists from Indonesia, Malaysia, Singapore and beyond to join the push to carve out an East Asian "Wilaya", or Islamic State province.
Hapilon had teamed up with the Maute clan in their stronghold of Lanao del Sur, one of the Philippines' poorest provinces, and brought with him fighters from his radical faction of Abu Sayyaf, a group better known for banditry.
Defence secretary Delfin Lorenzana, who estimated Marawi operations to have cost 5 billion pesos (Dh357.7m), 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.
The Marawi occupation set alarm bells ringing in the Philippines, with militants surprising 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.