Turkish forces loyal to President Tayyip Erdogan largely crushed an attempted military coup after crowds answered his call to take to the streets in support of the government and dozens of rebels abandoned their tanks.
Turkish officials say the death toll following the unrest has risen to 265 people, 161 people who are mostly civilians as well as 104 coup supporters.
The violence broke out after a faction of the armed forces tried to seize power using tanks and attack helicopters. Some strafed the headquarters of Turkish intelligence and parliament in the capital, Ankara, and others seized a major bridge in Istanbul.
President Erdogan accused the coup plotters of trying to kill him, and launched a purge of the armed forces, which last used force to stage a successful coup more than 30 years ago.
"They will pay a heavy price for this," said President Erdogan. "This uprising is a gift from God to us because this will be a reason to cleanse our army." A Turkish broadcaster reported that a purge of the judiciary was also under way.
At one stage military commanders were held hostage by the plotters and by Saturday evening - 24 hours after the coup was launched - some operations against rebels were continuing.
Foreign Minister Mevlut Cavusoglu said soldiers at the Incirlik air base, used by the United States to launch airstrikes on so-called Islamic State targets in Syria, were involved in the attempt. He said Turkey would resume operations with the US-led coalition once the anti-coup operations were completed.
The government declared the situation under control, saying 2,839 people had been rounded up, from foot soldiers to senior officers, including those who formed "the backbone" of the rebellion.
Anadolu news agency said one of those detained was the commander of the Second Army which protects the country's borders with Syria, Iraq and Iran.
In a rare show of unity, Turkey's four parties condemn coup
Turkey's four main political parties condemned the attempted military coup in a joint statement, marking a rare departure from usually fractious politics.
The four parties, which run the gamut from the right-wing, Islamist-rooted AK Party founded by President Erdogan to the left-of-centre, pro-Kurdish Peoples' Democratic Party (HDP), said their stance was invaluable for democracy in Turkey.
The statement was read aloud in the assembly by the parliamentary speaker.
In pictures: Attempted coup foiled in Turkey
EU's Tusk: 'No alternative' to democracy in Turkey
A successful overthrow of President Erdogan, who has ruled the country of about 80 million people since 2003, would have marked another seismic shift in the Middle East, five years after the Arab uprisings erupted and plunged Turkey's southern neighbour Syria into civil war.
However, a failed coup attempt could still destabilise a NATO member that lies between the European Union and the chaos of Syria, with so-called Islamic State bombers targeting Turkish cities and the government also at war with Kurdish separatists.
Mr Erdogan, who had been holidaying on the southwest coast when the coup was launched, flew into Istanbul before dawn and was shown on television outside Ataturk Airport.
Addressing a crowd of thousands of flag-waving supporters at the airport later, he said the government remained at the helm, although disturbances continued in Ankara.
Mr Erdogan, a polarising figure whose Islamist-rooted ideology lies at odds with supporters of modern Turkey's secular principles, said the plotters had tried to attack him in the resort town of Marmaris.
"They bombed places I had departed right after I was gone," he said. "They probably thought we were still there."
Mr Erdogan's AK Party has long had strained relations with the military, which has a history of mounting coups to defend secularism although it has not seized power directly since 1980.
His conservative religious vision for Turkey's future has also alienated many ordinary citizens who accuse him of authoritarianism.
Police used heavy force in 2013 to suppress mass protest demanding more freedom.
However, he also commands the admiration and loyalty of millions of Turks, particularly for restoring order to an economy once beset by regular crises.
Living standards have risen steadily under his rule, and while the economy has hit serious problems in recent years, it grew a greater-than-expected 4.8% year-on-year in the first quarter.
Still, the violence is likely to hit a tourism industry already suffering from the bombings and a row with Russia that had appeared to have been settled, and business confidence is also vulnerable.
Gunfire and explosions had rocked both Istanbul and Ankara through the night after soldiers took up positions in both cities and ordered state television to read out a statement declaring they had taken power.
However, by dawn the noise of fighting had died down considerably.
About 50 soldiers involved in the coup surrendered on one of the bridges across the Bosphorus Strait in Istanbul after dawn, abandoning their tanks with their hands raised in the air.
Turkey: Troops on the Bosphorus bridge surrender after attempting a coup.https://t.co/eNhezzQiXo
— RTÉ News (@rtenews) July 16, 2016
Neighbouring Greece arrested eight men aboard a Turkish military helicopter which landed in the northern city of Alexandroupolis, the Greek police ministry said, adding that they had requested political asylum.
In the early hours, lawmakers were hiding in shelters inside the parliament building, which was being fired on by tanks.
But momentum turned against the coup plotters as the night wore on. Crowds defied orders to stay indoors, gathering at major squares in Istanbul and Ankara, waving flags and chanting.
The government blamed the attempted coup on supporters of US-based cleric Fethullah Gulen.
Mr Gulen, a former ally of Mr Erdogan, denied being behind the attempted coup and condemned it "in the strongest terms".
Mr Erdogan has accused the reclusive Islamic preacher, who lives in a tiny town in the Pocono Mountains of the US state of Pennsylvania, of being behind the coup.
NATO chief hails 'strong support' of democracy by Turks
NATO chief Jens Stoltenberg has hailed the mass turnout of Turks on the streets overnight which played a critical role in thwarting the attempted coup.
"I welcome the strong support shown by the people and all political parties to democracy and to the democratically elected government of Turkey," Mr Stoltenberg said on Twitter.
The NATO chief called for "calm, restraint & full respect for Turkey's democratic institutions and constitution."
Mr Erdogan urged Turks to remain on the streets today.
Flights from Dublin Airport cancelled
Two Turkish Airlines flights to Istanbul due to depart from Dublin Airport today have been cancelled.
A spokesperson for Sunway Travel, which operates flights to Turkey, has said anyway travelling with them to Izmir are being offered the option of cancelling their holiday with a full refund, or they can continue with their travel plans.
US agency bans flights from Turkey to United States
Federal Aviation Administration has prohibited all airlines from flying from Turkey to the United States, it said today, following the failed coup.
The agency also issued a notice banning US commercial and private aircraft from flying to Turkey.
"The FAA is monitoring the situation in Turkey in coordination with our partners in the State Department and the Department of Homeland Security and will update the restrictions as the situation evolves," it said in a statement.