Forces loyal to the Turkish government fought on Saturday to crush the remnants of a military coup attempt which crumbled after crowds answered President Tayyip Erdogan's call to take to the streets and dozens of rebels abandoned their tanks.
More than 90 people were killed in violence that erupted late on Friday after a faction of the armed forces attempted to seize power using tanks and attack helicopters, some strafing the headquarters of Turkish intelligence and parliament in Ankara, others seizing a major bridge in Istanbul.
Erdogan appeared to accuse the coup plotters of trying to kill him, and promised to purge the armed forces which in the past have staged a number of successful coups. "They will pay a heavy price for this," he said. "This uprising is a gift from God to us because this will be a reason to cleanse our army."
Turkish authorities have already detained around 1,500 members of the armed forces, officials said. Chief of staff Hulusi Akar, who had been reported held hostage by the rebels, has been rescued, a senior official said.
A successful overthrow of Erdogan, who has ruled Turkey since 2003, would have marked one of the biggest shifts in the Middle East in years, transforming a major US ally while war rages on its border.
However, a failed coup attempt could still destabilise a NATO member that lies between the European Union and the chaos of Syria, with Islamic State bombers targeting Turkish cities and the government also at war with Kurdish separatists.
Erdogan, who had been holidaying on the southwest coast when the coup was launched, flew into Istanbul before dawn on Saturday and was shown on TV outside Ataturk Airport.
Addressing a crowd of thousands of flag-waving supporters at the airport later, Erdogan said the government remained at the helm, although disturbances continued in Ankara.
Erdogan, whose Islamist-rooted ideology lies deeply 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."
The death toll has risen to 90, state-run Anadolu Agency reported, with 1,154 people wounded.
Turkish authorities have detained 1,563 military personnel across the country.
Smart phone address
In a night that sometimes verged on the bizarre, Erdogan used social media to speak to the Turkish people — even though he is an avowed enemy of such technology when his opponents use it, frequently targeting Twitter and Facebook.
At one point Erdogan effectively addressed the nation via a video calling service, appearing on the smart phone of a CNN Turk reporter who held it up to a studio camera so that viewers to the network could see him.
The pro-coup faction said in an emailed statement from the Turkish military General Staff's media office address that it was determinedly still fighting. Calling itself the Peace at Home Movement, the faction also called on people to stay indoors for their own safety.
Prime Minister Binali Yildirim said rebel soldiers who had taken control of military aircraft had fired from the air and fighter jets had been scrambled to intercept them.
Gunfire and explosions had rocked both Istanbul and Ankara in a chaotic 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.
Around 50 soldiers involved in the coup surrendered on one of the bridges across the Bosphorus Strait in Istanbul after dawn on Saturday, abandoning their tanks with their hands raised in the air. Reuters witnesses saw government supporters attack the pro-coup soldiers who had surrendered.
Earlier, around 30 pro-coup soldiers had surrendered their weapons after being surrounded by armed police in Istanbul's central Taksim square.
They were taken away in police vans as a fighter jet repeatedly screeched overhead at low altitude, causing a boom that shook surrounding buildings and shattered windows.
Lawmakers in hiding
The coup began with warplanes and helicopters roaring over Ankara and troops moving in to seal off the bridges over the Bosphorus Strait that links Europe and Asia in Istanbul.
Authorities had shut the strait to tanker traffic, shipping agent GAC said.
In the early hours of Saturday, lawmakers were hiding in shelters inside the parliament building, which was being fired on by tanks. Smoke rose up from nearby, Reuters witnesses said. An opposition MP told Reuters parliament was hit three times and that people had been wounded.
A senior Turkish official said later on Saturday attacks on the parliament had "largely stopped".
A Turkish military commander also said fighter jets had shot down a helicopter used by the coup plotters over Ankara. State-run Anadolu news agency said 17 police were killed at special forces headquarters there.
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.
"We have a prime minister, we have a chief of command, we're not going to leave this country to degenerates," shouted one man, as groups of government supporters climbed onto a tank near Ataturk airport.
Erdogan and other officials blamed the attempted coup on followers of Fethullah Gulen, an influential cleric in self-imposed exile in the United States who once supported Erdogan but became a nemesis.
The pro-Gulen Alliance for Shared Values said it condemned any military intervention in domestic politics.
US Secretary of State John Kerry said he phoned the Turkish foreign minister and emphasised "absolute support for Turkey's democratically elected, civilian government and democratic institutions".
European Council President Donald Tusk called for a swift return to Turkey's constitutional order, saying tensions there could not be resolved by guns.
Flag carrier Turkish Airways resumed flights on Saturday, Erdogan said. Malaysia Airports, the operator of Sabiha Gokcen International Airport, Istanbul's second airport, said it would continue to process flights in and out of Turkey.
Soldiers took control of TRT state television, which announced a countrywide curfew and martial law. An announcer read a statement on the orders of the pro-coup faction that accused the government of eroding the democratic and secular rule of law. Turkey would be run by a "peace council" that would ensure the safety of the population, the statement said.
TRT went off the air shortly afterwards. It resumed broadcasting in the early hours of Saturday.
Reuters reporters saw a helicopter open fire in Ankara. Anadolu news agency said military helicopters had fired on the headquarters of the intelligence agency.
Turkey is one of the main backers of opponents of Syrian President Bashar al-Assad in that country's civil war, host to 2.7 million Syrian refugees and launchpad last year for the biggest influx of migrants to Europe since World War Two.
Celebratory gunfire erupted in Syria's capital Damascus after the army claimed to have toppled Erdogan. People took to the streets to celebrate there and in other government-held cities.
Turkey has suffered numerous bombings and shootings this year, including an attack two weeks ago by Islamists at Ataturk airport that killed more than 40 people, as well as those staged by Kurdish militants.
After serving as prime minister from 2003, Erdogan was elected president in 2014 with plans to alter the constitution to give the previously ceremonial presidency far greater executive powers.
Turkey has enjoyed an economic boom during his time in office and has dramatically expanded its influence across the region. However, opponents say his rule has become increasingly authoritarian.
His AK Party, with roots in Islamism, has long had a strained relationship with the military and nationalists in a state that was founded on secularist principles after World War One. The military has a history of mounting coups to defend secularism, but has not seized power directly since 1980.