ANKARA, Turkey -- Forces loyal to Turkey's president quashed a coup attempt in a night of explosions, air battles and gunfire that left dozens dead Saturday. Authorities arrested thousands of people as President Recep Tayyip Erdogan vowed those responsible "will pay a heavy price for their treason to Turkey."
The chaos capped a period of political turmoil in Turkey -- a NATO member and key Western ally in the fight against the Islamic State group -- which critics blamed on Erdogan's increasingly authoritarian rule. He has shaken up the government, cracked down on dissidents and opposition media and renewed conflict in the mainly Kurdish areas of the southeast.
Turkey has also been under strain from millions of refugees from the conflict in neighboring Syria and a series of deadly attacks blamed on the Islamic State group and Kurdish rebels.
Erdogan was on a seaside vacation when tanks moved into the streets of Ankara and Istanbul but flew home early Saturday.
Gen. Umit Dundar, the newly appointed acting chief of the general staff, said officers from the Air Force, the military police and the armored units were mainly involved in the attempt. 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.
Turkish Prime Minister Benali Yildirim said 161 people had been killed and 1,440 wounded in the overnight violence. He said 2,839 plotters had been detained.
Yildirim described the night as "dark stain for Turkish democracy" and said the perpetrators "will receive every punishment they deserve."
Turkey's NATO allies lined up to condemn the coup. U.S. President Barack Obama urged all sides to support Turkey's democratically elected government. NATO Secretary-General Jens Stoltenberg said he spoke to Turkish Foreign Minister Mevlut Cavusoglu and called for respect for democracy.
The coup attempt began late Friday, with a statement from the military saying it had seized control "to reinstall the constitutional order, democracy, human rights and freedoms, to ensure that the rule of law once again reigns in the country, for law and order to be reinstated."
Fighter jets buzzed overhead, gunfire erupted outside military headquarters and vehicles blocked two major bridges in Istanbul. Soldiers backed by tanks blocked entry to Istanbul's airport for a couple of hours before being overtaken by pro-government crowds carrying Turkish flags, according to footage broadcast by the Dogan news agency.
But the military did not appear unified, as top commanders went on television to condemn the action and order troops back to their barracks.
Erdogan, appearing on television over a mobile phone, had urged supporters into the streets to defend the government - and large crowds heeded the call.
People faced off with troops that had blocked the bridges over the Bosporus, linking the Asian and European sides of Istanbul.
In images broadcast on CNN-Turk, dozens of soldiers walked among tanks with their hands held up, surrendering to government forces. Discarded gear was strewn on the ground. Some flag-waving people climbed onto the tanks.
By early Saturday, the putsch appeared to have fizzled out, as police, soldiers and civilians loyal to the government confronted coup plotters.
Colonels and generals implicated in the rebellion were fired and loyal troops rescued the military chief who had been taken hostage at an air base on the outskirts of Ankara.
Addressing large crowds after landing at Ataturk airport, Erdogan said of the plotters: "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. They won't succeed as long as we stand against them by risking everything."
Fighting continued into the early morning, with the sounds of huge blasts echoing across Istanbul and the capital, Ankara, including at least one bomb that hit the parliament complex. Television footage showed images of broken glass and other debris strewn across a lobby leading to the assembly hall.
CNN-Turk said two bombs hit near the presidential palace, killing five people and wounding a number of others.
Prime Minister Yildirim called all legislators for an emergency meeting Saturday, Anadolu reported.
Turkey is a key partner in U.S.-led efforts to defeat the Islamic State group, and has allowed American jets to use its Incirlik air base to fly missions against the extremists in nearby Syria and Iraq. A coup against the democratically elected government could make it difficult for the United States to continue to cooperate with Turkey.
But Erdogan's Islamist government has also been accused of playing an ambiguous role in Syria. Turkey's renewed offensive against Kurdish militants -- who seek an autonomous state and are implacable foes of IS -- has complicated the fight against the Islamic State group.
Government officials blamed the coup attempt on a U.S.-based Islamist cleric, Fethullah Gulen.
Yildirim pinned blame on the "parallel terrorist organization" -- a term used by authorities to describe Gulen's movement.
Erdogan has long accused the cleric and his supporters of attempting to overthrow the government. The cleric lives in exile in Pennsylvania and promotes a philosophy that blends a mystical form of Islam with staunch advocacy of democracy, education, science and interfaith dialogue.
Fadi Hakura, a Turkey expert at the Chatham House think tank, said it was not clear who was behind the attempted coup, but it appeared to have been "carried out by lower-ranking officers -- at the level of colonel."
"Their main gripe seems to have been President Erdogan's attempt to transform his office into a powerful and centralized executive presidency," Hakura said.
But, he said, "this coup failed because it lacked popular support, political support and international support."
Turkey's military staged three coups between 1960 and 1980 and pressured Prime Minister Necmettin Erbakan, a pious Muslim mentor of Erdogan, out of power in 1997.
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. Erdogan's government has taken steps, including dismissals and prosecutions of high-ranking active and former officers for alleged coup plots, to bring the military to heel.
Soguel reported from Istanbul. Emrah Gurel and Cinar Kiper in Istanbul and Jill Lawless in London also contributed.