Image copyrightREUTERS
Image captionPresident Erdogan (centre) said the coup-plotters would pay a heavy price
Some 2,839 soldiers, including high-ranking officers, have been arrested over an attempted coup that is now over, says Turkey's PM Binali Yildirim.
In a night he called a "black stain on Turkish democracy", he said 161 people had been killed and 1,440 wounded.
Explosions and gunfire were heard in Ankara, Istanbul and elsewhere overnight and thousands of Turks heeded President Erdogan's call to rise up against the coup-plotters.
It is not clear who is behind the coup.
President Recep Tayyip Erdogan blamed a "parallel structure", in a clear reference to
Fethullah Gulen, a powerful but reclusive US-based Muslim cleric he accuses of fomenting unrest.
However, in a statement, Mr Gulen rejected any suggestion he had links to the events, saying he condemned "in the strongest terms, the attempted military coup in Turkey".
Image captionThe surrender of soldiers in Istanbul was captured live on television
International reaction
In pictures: Turkey coup attempt
Why did Turkish coup plot fail?
Events began on Friday evening when tanks took up positions on two bridges over the Bosphorus Strait in Istanbul, blocking it to traffic. Troops were seen on the streets and low-flying military jets were filmed over Ankara.
Shortly after, a faction of the army released a statement saying that a "peace council" was running the country, and there would be a curfew and martial law.
Jump media player
Media player help
Out of media player. Press enter to return or tab to continue.
Media caption"Here in Turkey there is confusion", reports the BBC's Katy Watson
The group said it had launched the coup "to ensure and restore constitutional order, democracy, human rights and freedoms".
It said that the democratic and secular rule of law had been eroded by the current government, and there would be a new constitution.
President Erdogan was in the south-west holiday resort of Marmaris at the time. He made a televised address, via his mobile phone, urging people to take to the streets to oppose the uprising.
He then flew on to Istanbul, saying Marmaris had been bombed after he left.
In a speech at Istanbul airport, Mr Erdogan said: "What is being perpetrated is a treason and a rebellion. They will pay a heavy price."
Image copyrightGETTY IMAGES
Image captionMr Erdogan spoke to CNN Turk via Facetime, urging supporters to oppose the coup
Image copyrightAFP
Image captionThere were fierce clashes overnight in Istanbul and Ankara
Image copyrightAFP
Image captionMany people were injured
Outbreaks of violence
The Turkish parliament and presidential buildings in Ankara were attacked. At least one bomb hit the parliament complex. MPs were believed to be hiding in shelters.
Gunfire was also heard outside Istanbul police headquarters and tanks were said to be stationed outside Istanbul airport.
Broadcaster CNN Turk was temporarily taken off air after soldiers entered the building and tried to take it over. CNN Turk later
tweeted a photo of soldiers being arrested by police.
Image copyrightTWITTER
Mike Baddeley, on holiday in Marmaris, said he was woken by "a very large explosion, followed by, it seemed like one or two helicopters flying above our heads... with machine gun fire".
In the morning, he saw armed men in military fatigues walking around the hotel, but no further violence.
There were reports of fierce clashes in Taksim Square in the centre of Istanbul, and gunfire and explosions were heard near the square.
One of the helicopters being flown by rebels was shot down by government troops in Ankara.
Sporadic gunfire was still being reported in some areas by morning.
What is happening now?
Prime Minister Yildirim said the situation was now "completely under control" and the government's commanders were now back in charge.
Earlier, the acting military chief of staff Umit Dundar said officers from the air force, the military police and armoured units had been mainly involved in the coup attempt.
He said that although the chief of staff had been rescued, several military commanders were still being held hostage.
Around 200 unarmed soldiers have left Turkey's military headquarters in the capital Ankara and surrendered to police, the state-run Anadolu news agency says.
Earlier, dramatic images showed dozens of soldiers walking away from their tanks with their hands up on one of Istanbul's Bosphorus bridges, after they had closed it off to traffic all night.
World reaction
In Washington, President Barack Obama urged all parties in Turkey to support the "democratically elected government".
Nato, of which Turkey is a member, called for "full respect" for Turkey's democratic institutions.
European Council President Donald Tusk said the country was "a key partner for the European Union" and called for a "swift return to Turkey's constitutional order".
Russia said it was deeply concerned, saying "the flare-up of the domestic political situation against the backdrop of the existing terrorist threats in this country and the armed conflict in the region brings a heightened risk to international and regional stability".
Iran said events in Turkey proved "that a coup d'etat has no place and is doomed to fail in our region".
Image copyrightREUTERS
Image captionErdogan supporters have been celebrating the surrender of soldiers in Istanbul
Image copyrightREUTERS
Image captionSurrendered soldiers had to be protected by police officers from Erdogan supporters
Turkey's military coups
- 1997 - also known as "post-modern coup". Turkish military intervention leads to resignation of Islamist prime minister Necmettin Erbakan.
- 1980 - Military coup following armed conflict between right-wing and left-wing groups in the 1970s
- 1971 - Military coup known as the "coup by memorandum", which the military delivered instead of sending out tanks
- 1960 - Coup by group of young military officer outside chain of command, against the democratically-elected Democrat Party
Source: http://www.bbc.com/news/world-europe-36813924