FORCES loyal to Turkey’s president quashed a coup attempt in a night of explosions, air battles and gunfire that left at least 265 people dead and 1,440 wounded Saturday. Authorities arrested thousands as President Recep Tayyip Erdogan vowed that those responsible “will pay a heavy price for their treason.”

The chaos came amid a period of political turmoil in Turkey — a NATO member and key Western ally in the fight against the Islamic State group — that critics blame on Erdogan’s increasingly authoritarian rule.

Staying in power by switching from being prime minister to president, Erdogan has shaken up the government, cracked down on dissidents, restricted the news media and renewed fighting with Kurdish rebels.

The government has also come under pressure from the millions of refugees in Turkey who have fled violence in neighboring Syria and Iraq, and a series of bloody attacks in Turkey blamed on the Islamic State group and Kurdish rebels. Erdogan was on a seaside vacation when tanks rolled into the streets of Ankara and Istanbul. He flew home early Saturday and declared the coup to have failed.

“They have pointed the people’s guns against the people. The president, whom 52 percent of the people brought to power, is in charge. This government brought to power by the people is in charge,” Erdogan told large crowds after landing at Istanbul’s Ataturk Airport.

The uprising appears not to have been backed by the most senior ranks of the military, and Turkey’s main opposition parties quickly condemned the attempted overthrow of the government. Gen. Umit Dundar said the plotters were mainly officers from the Air Force, the military police and the armored units.Prime Minister Binali Yildirim said 161 people were killed and 1,440 wounded in the violence, and 2,839 plotters were detained. A source at the office of the presidency, speaking on condition of anonymity in line with government rules, said the toll of 161 “excludes assailants” — which could mean the death toll is much higher.

Yildirim described the night as “a black mark on Turkish democracy” and said the perpetrators “will receive every punishment they deserve.”

Turkey’s NATO allies lined up to condemn the coup. President Barack Obama urged all sides to support Turkey’s democratically elected government. NATO S e c r e t a r y – G e n e r a l Jens Stoltenberg said he spoke to Turkish Foreign Minister Mevlut Cavusoglu and called for the Turkish people to respect democracy.

There have long been tensions between the military — which saw itself as the protector of the secular Turkish state — and Erdogan’s Islamic-influenced AKP party.