Filipino militants aligned with the self-styled "Islamic State" (IS) retreated Wednesday after spending hours holed up in a school in Malagakit village in North Cotabato province.
Officials said at least four rebels were killed and two government militiamen were wounded in the fighting.
"It's already resolved," Brigadier General Restituto Padilla said. "They've withdrawn. They are no longer there. The school area is safe. The troops are in pursuit mode."
Padilla said 31 hostages were recovered, but the militants apparently took five civilians with them. "We are still verifying if they have not been released," he said.
The gunmen, believed to be members of the Bangsamoro Islamic Free Fighters (BIFF), stormed the building early Wednesday while the school was closed.
Padilla said the rebels may have been seeking to exploit the massive military offensive in Marawi by plotting attacks elsewhere.
"They were taking advantage of the situation that we have a very lightly defended outpost and that they think our forces are elsewhere in the province," Padilla said. "But that is not the case, our forces are spread all over. We may be facing many fronts ... but we've properly assigned our forces."
The militant uprising in Marawi prompted President Rodrigo Duterte to declare martial law across the Mindanao region last month.
Manila has deployed ground troops and dispatched helicopters, which have carried out rocket strikes against positions held by IS-linked groups. On Tuesday, government forces launched a renewed push to secure Mawari city.
dv/sms (AFP, AP, Reuters, dpa)