The government is also under pressure from hosting millions of refugees who have fled the violence in neighboring Syria and Iraq, and from a series of bloody attacks blamed on Islamic State extremists and Kurdish rebels.
Erdogan was on a seaside vacation when tanks rolled into the streets of Ankara and Istanbul overnight Friday, blocking key bridges. From a cellphone, he delivered a televised address that called for huge crowds to come out and defend Turkey's democracy - which they did in Ankara, the capital, and in Istanbul, facing off against troops who had blocked key Bosporus bridges that link the city's Asian and European sides.
"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," he told large crowds after landing at Istanbul's Ataturk Airport early Saturday and declaring the coup a failure.
Prime Minister Binali Yildirim described the night as "a black mark on Turkish democracy" and said the perpetrators "will receive every punishment they deserve." He said July 15 will be remembered as "a festival for democracy," the day when those who carried out a coup against the people were hit by a coup themselves.
Late Saturday, Defense Minister Fikri Isik said state authorities were in full control of all areas in Turkey but warned that authorities would remain vigilant.
The uprising appears not to have been backed by the most senior ranks of the military. Gen. Umit Dundar said the plotters were mainly officers from the Air Force, the military police and the armored units.
Turkey's four main political parties released a joint declaration during an extraordinary parliamentary meeting Saturday denouncing the coup attempt and declaring that any further moves against the people or parliament would be met "with the iron will of the Turkish Grand National Assembly resisting them."
The statement praised Turks for taking to the streets and resisting the coup.
The death toll from the unrest appeared to be around 265 people. Yildirim said 161 people were killed and 1,440 wounded in the process of putting down the coup attempt 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 161 "excludes assailants." Dundar said at least 104 "coup plotters" had died.
Turkey's NATO allies lined up to condemn the coup attempt. President Barack Obama and German Chancellor Angela Merkel urged all sides to support Turkey's democratically elected government and Obama held a meeting with his national security advisers. NATO Secretary-General Jens Stoltenberg spoke to Turkish Foreign Minister Mevlut Cavusoglu and urged the Turkish people to respect democracy.
U.S. airline regulators banned all flights between the U.S. and airports in Ankara and Istanbul, including flights to the U.S. via third countries.
While government officials blamed the coup attempt on Gulen, the cleric said he condemned "in the strongest terms, the attempted military coup in Turkey" and sharply rejected any responsibility for it.
"Government should be won through a process of free and fair elections, not force," Gulen said. "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."
U.S. Secretary of State John Kerry said Saturday the Obama administration would entertain an extradition request but Turkey would have to prove wrongdoing by Gulen.
The cleric, who left Turkey in 1999, now 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.
"We would invite the government of Turkey, as we always do, to present us with any legitimate evidence that withstands scrutiny. And the United States will accept that and look at it and make judgments about it appropriately," Kerry said.
Even before the unrest was under control, Erdogan's government pressed ahead Saturday with a purge of Turkish judicial officials, with 2,745 judges being dismissed across Turkey for alleged ties to Gulen, according to the state-run Anadolu news agency. It said 10 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.
Among those detained for questioning was the commander of Turkey's second army, Gen. Adem Huduti, and other top aides in the eastern city of Malatya, Anadolu said.
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.
Top military commanders went on television to condemn the action and order troops back to their barracks. By early Saturday, the putsch appeared to have fizzled.
CNN-Turk showed dozens of soldiers walking 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. The Hurriyet newspaper, quoting investigators, said some privates had thought they were on military maneuvers, not a coup attempt.
A Blackhawk military helicopter with seven Turkish military personnel and one civilian landed in the Greek city of Alexandroupolis, where the passengers requested asylum. 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 huge blasts echoing across Istanbul and Ankara, including at least one bomb that hit the parliament complex, scattering broken glass and other debris across a lobby. CNN-Turk said two bombs hit near the presidential palace, killing five people and wounding 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 Syria and Iraq. The Pentagon said U.S. warplanes stopped flying missions against IS after the Turkish government closed its airspace early Saturday to military aircraft, and U.S. officials were working with Turkey to resume air operations as soon as possible.
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 IS.
Fadi Hakura of the Chatham House think tank in London said the attempted coup appeared to have been "carried out by lower-ranking officers."
"Their main gripe seems to have been President Erdogan's attempt to transform his office into a powerful and centralized executive presidency," Hakura said. "In the short term, this failed coup plot will strengthen President Erdogan."
Turkey's military staged three coups between 1960 and 1980 and pressured Prime Minister Necmettin Erbakan, a pious mentor of Erdogan, out of power in 1997.