ILIGAN, Philippines — A Roman Catholic priest who was held hostage for months by Islamic State-inspired militants in the war-torn southern city of Marawi has been freed, the Philippine military said on Sunday, as it moved closer to rooting out the remaining gunmen from their strongholds.

The Rev. Teresito Suganob was rescued late Saturday by troops who cleared a mosque that militants had been using as a defensive post, the authorities said.

“He was rescued by our men on the ground,” said Jesus Dureza, a senior presidential adviser.

The commander of naval forces in the southern region, Rear Admiral Rene Medina, gave a somewhat different account, saying that the priest had managed to escape with another hostage amid the heavy fighting. They were picked up by the military and brought to a safer place, Admiral Medina said.

No information was immediately available about Father Suganob’s condition or that of the other hostage, a teacher.