The insurgents have withstood a relentless US-backed bombing campaign, raising awkward questions about the Philippines' ability to defend itself...
What began as a surprise takeover by militants that Manila said would be easily defeated, quickly descended into house-to-house fighting against a deceptively well-planned Islamist insurgency.
The insurgents have withstood a relentless US-backed bombing campaign, raising awkward questions about the Philippines' ability to defend itself from an increasingly assertive Islamist presence in its violence-plagued south.
Here are the key moments in the battle for Marawi that has left more than 1,000 people killed and 400,000 residents displaced, and the city in ruins.
Fighting broke out on May 23 during what the Philippine army said was an operation to catch Hapilon, wanted for years as leader of the Abu Sayyaf kidnap-for-ransom group and later as the declared regional leader of the so-called Islamic State.
It was clear the Philippine military were caught unprepared.
Seemingly unknown to them, Hapilon had joined forces with the Maute Group, a local Islamist insurgent network, and had been planning for months to seize Marawi, considered the Muslim capital of the largely Catholic Philippines.
The raid against Hapilon failed and pushed the jihadists into action. They swiftly seized key buildings and took hostages, forcing thousands to flee as troops surrounded the city.
Initially the military spoke in confident tones, vowing to liberate Marawi in time for Philippine Independence Day on June 12.
But as the weeks passed it was clear the militants had planned for a lengthy and suicidal siege, hiding arms caches across the city linked by a network of tunnels and passages between buildings.
They were also backed by hardened international jihadists, who the military said had snuck into the country from Indonesia, Malaysia and as far afield as Chechnya.
Sustained aerial and artillery bombardments, aided by Australian and US reconnaissance aircraft and intelligence, failed to dislodge the fighters.
In late July Philippine President Rodrigo Duterte extended martial law over the region until the end of 2017, admitting his forces were "having a hard time".
Throughout the conflict hundreds of hostages escaped or were freed as the fighting progressed, bringing with them harrowing testimony of their treatment at the hands of brutal captors.
Some recalled how Christians who were unable to recite the Quran were simply executed on the spot in the early stages of the takeover.
Authorities said the hostages' roles included carrying the gunmen's food and ammunition, serving as stretcher-bearers for their wounded, collecting munitions, acting as human shields and even being forced to fight.
In mid-September local Catholic priest Father Teresito Suganob, taken hostage on the first day of the fighting, made a dramatic escape as soldiers overran a mosque where he and others were being held.
At the time the military estimated there were still 40 to 60 hostages held by the militants.
Slowly, street by street and house by house, the militants were pushed back into an increasingly small patch of the pulverised city.
On Sunday, the military said the end was in sight. Then on Monday Philippine Defence Secretary Delfin Lorenzana said Hapilon had been killed in the closing stages of a ground assault alongside Omar Maute, a key leader in the allied Maute Group.
While authorities have made several previous announcements on the imminent end of the conflict, observers believe this time an end to the battle is drawing to a conclusion.
But the costs have been steep. One of the most important Muslim-majority cities in the south lies in ruins, its population traumatised and dispossessed.
According to authorities, some 822 militants, 162 government forces and 47 civilians have been killed in the fighting -- and dozens of insurgents remain in the battle area.