MANILA, Philippines — Philippine President Rodrigo Duterte fired a rifle toward Islamic State-linked militants during a visit Thursday to the battle zone in southern Marawi, after troops recaptured a main mosque where the gunmen had taken cover with their hostages in the three-month siege of the city, officials said.

Clad in a combat uniform, protective vest and helmet, Duterte congratulated the troops for regaining control of the Islamic Center, an indication they are entering the final stage in ending the disastrous uprising. It was Duterte’s third known trip to the embattled city.

During his brief visit, Duterte inspected a devastated community near the frontline and talked to troops guarding a recaptured building. He also visited a military patrol base and “tried a sniper rifle and fired twice toward the direction of the terrorists,” a government statement said.

Army Col. Romeo Brawner said Duterte went to the main battle area, a cluster of dense, mosque-dotted communities which has been heavily damaged in the fighting, with military chief Gen. Eduardo Ano and top commanders.

More than 760 people, including 595 militants, have died in the Marawi fighting, which has sparked concerns that the Islamic State group may have taken a foothold in Southeast Asia through local extremists as it suffers battle setbacks in Syria and Iraq.

About 600 gunmen launched the insurrection in Marawi’s commercial center on May 23 after a botched army raid to capture the group’s leader, Isnilon Hapilon, according to the military.

The United States and Australia have deployed surveillance aircraft to help Filipino troops locate the hundreds of militants who took positions in buildings, mosques and houses, some of them linked by underground tunnels. China provided heavy weaponry and Southeast Asian governments offered aid for troops and the hundreds of thousands of displaced residents.

It was not immediately clear if any militants or their hostages were in the mosque when troops entered the building Thursday after weeks of painstakingly slow advances because of sniper fire and an order from Duterte to avoid any massive attack that might harm an estimated several dozen hostages, including a Roman Catholic priest, used by the gunmen as human shields.

Brawner said the militants withdrew shortly before troops gained access to the mosque in fighting that wounded three soldiers. He said the gunmen had rigged the building with booby-traps and explosives which were being cleared by the troops.

“We recovered the mosque after some resistance but not through an assault with bombardment because we wanted to preserve the structure,” Brawner said.

The military says about 40 gunmen are still fighting in the main battle zone, now confined to a smaller cluster of communities after troops backed by airstrikes and artillery fire recaptured key bridges and crossed over a river toward the main militant position.

Some residents have started to return to neighborhoods declared safe by the military. Classes at the main Mindanao State University reopened Tuesday, although a clash between government forces and gunmen allied with the militants in nearby Marantao town, outside the city, underscored the remaining danger.

Duterte declared martial law in the largely Roman Catholic archipelago’s southern third until the end of the year to deal with the Marawi crisis and prevent other armed groups from reinforcing the militants or launching similar uprisings elsewhere in the volatile region.