ANKARA, TURKEY --Forces loyal to Turkey's president quashed a coup attempt in a night of explosions, air battles and gunfire that left at least 161 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."
PHOTOS: Unrest in Turkey
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 Secretary-General 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.
Government officials blamed the coup attempt on a U.S.-based moderate Islamic cleric, Fethullah Gulen. Erdogan has often accused the cleric and his supporters of attempting to overthrow the government. Gulen 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.
Gulen, however, said he condemned "in the strongest terms, the attempted military coup in Turkey" and sharply rejected any responsibility for the attempted coup.
"Government should be won through a process of free and fair elections, not force," he said. "I pray to God for Turkey, for Turkish citizens, and for all those currently in Turkey that this situation is resolved peacefully and quickly."
"As someone who suffered under multiple military coups during the past five decades, it is especially insulting to be accused of having any link to such an attempt. I categorically deny such accusations," he added.
Still, the government pressed ahead Saturday with a purge of judicial officials, with 2,745 judges being dismissed across Turkey for alleged ties to Gulen. Ten members of Turkey's highest administrative court were detained and arrest warrants were issued for 48 administrative court members and 140 members of Turkey's appeals court, state media reported.
The coup attempt began late Friday, with a military statement saying forces 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.
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 his government, and large crowds heeded his call. People faced off against troops who had blocked key bridges over the Bosporus that link the Asian and European sides of Istanbul.
By early Saturday, the putsch appeared to have fizzled, as police, soldiers and civilians loyal to the government confronted coup plotters.
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.
NTV television showed a Turkish colonel and other soldiers on their knees being searched and taken into custody at military headquarters. The Hurriyet newspaper, quoting investigators, said some privates told them they were not even aware they were part of a coup attempt but thought they were on military maneuvers.
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, the capital.
A Blackhawk military helicopter with seven Turkish military personnel and one civilian landed in the Greek city of Alexandroupolis, where the passengers requested asylum, according to Greece's defense ministry. While Turkey demanded their extradition, Greece said it would hand back the helicopter and consider the men's asylum requests.
Fighting continued into the early morning, with the sounds of huge blasts echoing across Istanbul and Ankara, including at least one bomb that hit the parliament complex. Television footage showed 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.
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 have made it difficult for the United States to continue to cooperate with Turkey.
Erdogan's Islamist government has also been accused of playing an ambiguous - even double-sided - role in Syria. Turkey's renewed offensive against Kurdish militants - who seek more autonomy and are implacable foes of IS - has complicated the U.S.-led fight against the Islamic State group.
Fadi Hakura, a Turkey expert at the Chatham House think tank in London, 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. "I think in the short term this failed coup plot will strengthen President Erdogan, particularly in his drive to turn his office into a strong and centralized executive presidency."
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.