ANKARA, Turkey - A violent, chaotic night in Turkey ended with at least 161 people dead following an attempted coup; and President Recep Tayyip Erdogan's reappearance after hours of uncertainty.
Prime Minister Binali Yildirim said at a press conference Saturday the situation was under control, with all commanders back to work.
"Our nation in this incident has shown a great resilience," Yildirim said.
"Those who have done this uprising should understand this reality that no one can play games with the stability of this country and the love of freedom and democracy."
An additional 1,140 people were wounded, said Yildirim.
A total of 2,839 military officers were detained, a source in the President's office said.
As the morning wore on, crowds emptied out of Istanbul's Taksim Square, where many gathered the night before.
Of the 161 people dead, most were police officers killed in a gunfire exchange with a helicopter near the Parliament complex in Ankara, Turkey's NTV reported. It said the building was damaged.
The alleged coup plotters are in control of at least one air force base, according to the Interior Ministry.
In Istanbul, a defiant Erdogan addressed crowds, telling them that the coup had been quashed.
"The government is in control," he told supporters as they chanted his name. "Fifty percent of the people elected the President and that President is on duty."
He said those involved will be dealt with.
"So far as we believe, so far as we're alive, we'll be prepared to die in the cause to tackle these people ... we're not going to compromise."
Shortly after dawn, video footage showed soldiers surrendering en masse. At least 200 soldiers turned themselves in to police in Ankara, Turkish state media reported.
They walked away from tanks and abandoned their posts on the Bosphorus Bridge, which connects the European and Asian sides of Istanbul.
Turkish Airlines resumed flights out of Ataturk, which had earlier been overrun by protesters.
Opposition soldiers had attempted to seize control overnight in various locations across the country, including the capital Ankara.
Erdogan, who traveled overnight from the seaside resort of Marmaris, addressed the country from Istanbul Ataturk Airport. He called the attempt "treason."
Erdogan took the forces he apparently suspects of masterminding the coup to task.
"Now I'm addressing those in Pennsylvania," he said, in an apparent reference to Fethullah Gulen, a cleric and former ally who lives in exile in Pennsylvania.
"The betrayal you have shown to this nation and to this community, that's enough. If you have the courage, come back to your country. If you can. You will not have the means to turn this country into a mess from where you are."
In a statement, Gulen denied any connection to the coup attempt and said he condemned it.
Witness Katherine Cohen, an American who's staying in an Istanbul hotel, said she heard a loud explosion at sunrise, and gunfire and jets all through the night.
For much of the night, fighter jets flew low over Istanbul while armored vehicles streamed across a main bridge in the city. Gunshots rang out on Bosphorus Bridge, sending pro-government protesters to the ground.
In Ankara, gunfire rang overnight as jets circled above.
"When I stuck my head out, I could see helicopters shooting," said Diego Cupolo, a photojournalist in Ankara.
He said he could see tracer rounds zip through the air.
Bombs were thrown at the Parliament building in Ankara. A helicopter the government says was stolen by coup plotters was shot down by an F-16.
Chaos in the streets
Tens of thousands of protesters took to the street after Erdogan's call to confront the military was broadcast on television.
Many waved Turkish flags and chanted their support for the President. Some climbed on tanks and blocked the path of military vehicles with their cars. But some soldiers got hugs from apparent supporters.
Ömer Çelik, Turkey's EU negotiator, tweeted images that he said showed some of the damage at the Turkish Parliament:
"Our country has been subjected to treacherous enemy attack, which displays betrayal to the nation, their uniforms and morals. The necessary response has been shown to the enemy and it is still being shown," he said.
A fighter jet shot down a helicopter that had been commandeered by "coup plotters," a Turkish presidential source told CNN.
The Turkish military claim of a takeover was read by an anchor on state broadcaster TRT. She said the military imposed martial law.
The statement was made on behalf of the "Peace in the Nation" council, the announcer said. "The political administration that has lost all legitimacy has been forced to withdraw," the anchor said. Later, after the military was kicked out, she said she was forced to read the military statement. "We were taken over. I was forced by men with arms and they told us that they would not harm us if we did as told."
When the coup began, soldiers blocked two bridges in Istanbul between the European and Asian sides.
Erdogan's rise to power
Erdogan was elected Prime Minister in 2003. Under his rule, Turkey became a powerhouse in the Middle East. His reign came to an end in 2014, and his own party's rules prevented him from seeking a fourth term.
He ran for President -- and won. Before this, the President of Turkey was a largely ceremonial role, but Erdogan tried to change that by altering the constitution to give him more power.
Under Erdogan, who is extremely conservative, religion had started to play a more important role in Turkey, which is a largely secular country. He was active in Islamist circles in the 1970s and 1980s.
CNN's Gul Tuysuz, Kevin Bohn and Amanda Wills contributed to this report.