ISTANBUL/ANKARA - Forces loyal to Turkey's government fought on Saturday to crush the last remnants of a military coup attempt which collapsed after crowds answered President Tayyip Erdogan's call to take to the streets and dozens of rebels abandoned their tanks.
One hundred and sixty-one people were killed, including many civilians, 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.
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 Erdogan, who also saw off mass public protests against his rule three years ago. "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 underway.
One government minister said some military commanders were still being held hostage by the plotters. But the government declared the situation fully under control, saying 2,839 people had been rounded up from foot soldiers to senior officers, including those who had formed "the backbone" of the rebellion.
A successful overthrow of 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 and major U.S. ally 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 television outside Ataturk Airport.
Addressing thousands of flag-waving supporters at the airport later, he said the government remained at the helm, although disturbances continued in Ankara.
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."
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 percent year-on-year in the first quarter.
Still, the violence is likely to hit a tourism industry already suffering from the bombings, and business confidence is also vulnerable.
Smart phone address
In a night that sometimes verged on the bizarre, Erdogan frequently took to social media, even though he is an avowed enemy of the technology when his opponents use it and frequently targets Twitter and Facebook.
Erdogan 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.
He said the "parallel structure" was behind the coup attempt -- his shorthand for followers of Fethullah Gulen, a Muslim cleric whom he has repeatedly accused of trying to foment an uprising in the military, media and judiciary.
Gulen, who lives in self-imposed exile in the United States, once supported Erdogan but became a leading adversary. He condemned the attempted coup and said he played no role in it.
"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," Gulen said in a statement.
Secretary of State John Kerry said the United States had not received any request to extradite Gulen.
The purge appeared to go beyond the military. Citing a decision by the High Council of Judges and Prosecutors, broadcaster NTV reported that authorities had removed 2,745 judges from duty.
Soldiers surrender
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 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.
By Saturday afternoon, CNN Turk reported that security forces had completed an operation against coup plotters at the headquarters of the military general staff. Security sources also said police detained about 100 military officers at an air base in the southeast.
Neighbouring Greece arrested eight men aboard a Turkish military helicopter which landed in the northern city of Alexandroupolis on Saturday, the Greek police ministry said, adding that they had requested political asylum.
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, which separates Europe and Asia in Istanbul.
Turkish maritime authorities reopened the Bosphorus to transiting tankers after shutting the major trade route from the Black Sea to the Aegean for several hours for security and safety reasons.
In the early hours of Saturday, lawmakers were hiding in shelters inside the parliament building, which was being fired on by tanks. An opposition deputy 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.
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.
Kerry said he phoned the Turkish foreign minister and underlined "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. Erdogan has had often prickly relations with the EU, which worried about some of his actions including raids on Turkish media.
Flights resume
Flag carrier Turkish Airways resumed flights on Saturday, while some foreign carriers cancelled weekend flights.
At the height of the action, rebel 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. Turkeywould be run by a "peace council" that would ensure the safety of the population, the statement said.
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.
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.