PANTAR, Philippines (Reuters) - Thousands of civilians fled fighting in the Philippines on Wednesday as troops tried to fend off Islamist militants who took over large parts of a city, capturing Christians, seizing and torching buildings and setting free scores of prisoners.

Government troops check a vehicle evacuating residents from their hometown of Marawi city in southern Philippines, as it drives past a military checkpoint in Pantar town, Lanao Del Norte, Philippines May 24, 2017. REUTERS/Romeo Ranoco

Islamic State claimed responsibility for the rampage via its Amaq news agency, and President Rodrigo Duterte defended his decision to declare martial law on Mindanao, the Muslim-majority island where Marawi City is located, to prevent the spread of extremism in the impoverished region.

The violence flared in Marawi on Tuesday afternoon after a botched raid by security forces on a hideout of the Maute, a militant group that has pledged allegiance to Islamic State.

Fighters quickly dispersed, torching buildings and taking over bridges, a hospital, two jails, a church and a college. Duterte said he heard reports they may have beheaded a police chief.

He said Islamic State must be repelled from the Christian-majority Philippines and he would use all means possible to crush the Maute group and the allied Abu Sayyaf, whatever the consequences.

“Anyone now holding a gun, confronting government with violence, my orders are spare no one, let us solve the problems of Mindanao once and for all,” said Duterte, who is from the island, after cutting short a visit to Russia and returning to Manila.

“If I think you should die, you will die. If you fight us, you will die. If there’s an open defiance, you will die, and if it means many people dying, so be it. That’s how it is.”

Soldiers and guerrillas set up rival checkpoints and roadblocks on routes in and around Marawi as civilians fled the city of 200,000 in droves, leaving behind what one official described as a ghost town.

Long queues of pickup trucks and jeeps crammed full of people and loaded with belongings crawled along roads into nearby towns as troops searched vehicles for weapons and bombs.

The military said it had rescued 120 people from a school and a hospital and was trying to isolate Maute fighters while awaiting reinforcements that were being blocked by rebels.

Maute snipers and booby traps were hampering operations, which the army said could last three more days.

HUMAN SHIELDS

The Catholic church said militants were using Christians and a priest as human shields and had contacted cardinals with threats to execute hostages unless government troops withdrew.

Thirteen militants and seven security personnel have so far been killed and 33 troops wounded, the army said.

Mujiv Hataman, governor of the Autonomous Region in Mindanao, said militants freed 107 prisoners, among them Maute rebels.

Duterte said martial law would mean checkpoints and arrests and searches without warrant, and it would go on for as long as necessary.

He said he would consider some security measures in the central Visayas region next to Mindanao to facilitate arrests, and could even declare martial law nationwide. He was furious that militants had hoisted the Islamic State flag in Marawi.

Slideshow ( 10 images )

“I made a projection, not a prediction, that one of these days the hardest things to deal with would be the arrival of ISIS,” Duterte said, referring to Islamic State.

“The government must put an end to this. I cannot gamble with ISIS because they are everywhere.”

Duterte said he would not tolerate abuses of power by security forces under martial law, but critics said the military rule in all of Mindanao, an island the size of South Korea with a population of 22 million, was an overreaction.

The National Union of Peoples’ Lawyers, a group of human rights attorneys, called it “a sledgehammer, knee-jerk reaction” that would “open the flood gates for unbridled human rights violations”.

The military has not explained how the raid on an apartment hideout went so badly wrong. The operation was aimed at capturing Isnilon Hapilon, a leader of the Abu Sayyaf group notorious for piracy, banditry and for kidnapping and decapitating Westerners.

The Maute and Abu Sayyaf have proved fierce opponents for the military.

The armed forces said they were on top of the situation but residents who fled told a different story.

“The city is still under the control of the armed group. They are all over the main roads and two bridges leading to Marawi,” student Rabani Mautum told Reuters in Pantar town, about 16 km (10 miles) away.

Bishops and cardinals urged Islamic leaders to persuade militants to free innocent hostages.

“We beg every Filipino to pray fervently,” said Father Socrates Villegas, president of the Catholic Bishops Conference of the Philippines.