MANILA — Islamist militants seized an elementary school in the restive southern Philippines on Wednesday, holding 31 hostages, including students, for about 12 hours before escaping into a vast marshland area, the military said.

The militants, from a splinter group called the Bangsamoro Islamic Freedom Fighters, seized Malagakit Elementary School in the town of Pigcawayan as they were retreating from a nearby government outpost they had attacked, the military said.

Capt. Arvin Encinas, a military spokesman, said the gunmen took over the school at dawn on Wednesday, triggering skirmishes and the evacuation of about 1,000 residents in the area.

They took hostages, including six students, to prevent troops from advancing. Two pro-government militiamen were wounded in the firefight, the military said.