Turkey’s elected government on Saturday rounded up thousands of soldiers, including high-ranking officers, as it moved quickly to reassert control after a failed military coup to oust President Recep Tayyip Erdogan.

Turkey’s Prime Minister Binaldi Yildirim called the attempted coup a “black stain on Turkish democracy” that left 161 people dead and 1,440 wounded.

He said 2,839 military personnel had been detained as part of a crackdown on coup plotters.

Erdogan was on a seaside vacation when tanks rolled into the streets of Ankara and Istanbul. He flew home early Saturday and declared the coup to have failed.

"They have pointed the people's guns against the people. The president, whom 52 percent of the people brought to power, is in charge. This government brought to power by the people is in charge," Erdogan told large crowds after landing at Istanbul's Ataturk Airport.

Government officials blamed the coup attempt on a U.S.-based moderate Islamic cleric, Fethullah Gulen. Erdogan has often accused the cleric and his supporters of attempting to overthrow the government. Gulen lives in exile in Pennsylvania and promotes a philosophy that blends a mystical form of Islam with staunch advocacy of democracy, education, science and interfaith dialogue.

Gulen, however, said he condemned "in the strongest terms, the attempted military coup in Turkey" and sharply rejected any responsibility for the attempted coup.

Still, the government pressed ahead Saturday with a purge of judicial officials, with 2,745 judges being dismissed across Turkey for alleged ties to Gulen. Ten members of Turkey's highest administrative court were detained and arrest warrants were issued for 48 administrative court members and 140 members of Turkey's appeals court, state media reported.

After a Turkish military helicopter landed in Greece Saturday after sending a distress signal, the Turkish goverment demanded the prompt return of eight ‘treacherous officers’ on board the aircraft.

The eight were arrested in Greece for entering the country illegaly and were identified as two majors, a captain and five privates by Anadolu Agency, a state-run news organization.

After landing on Greek soil, they requested political asylum and Greek officials said they would consider the request. The officials also said they would return the helicopter to Turkey "as soon as possible."

The coup attempt was swift and came as a surprise to allies in the region. Forces loyal to Erdogan put down the coup attempt in a night of explosions, air battles and gunfire.

Erdogan vowed that those responsible "will pay a heavy price for their treason."

NTV television aired footage of a Turkish colonel and other soldiers being taken into custody at military headquarters.

The video shows them being hand-searched by special forces police, their hands behind their heads. Some are later seen kneeling on the ground, their hands still held behind their heads.

According to Hurriyet newspaper's online edition, some of the privates who were detained told interrogators they were not aware that they were part of a coup attempt. They had been told by commanders they were taking part in military maneuvers. Some said they understood that it was a coup attempt when they saw civilians climb on tanks.

Military chief of staff Gen. Hulusi Akar was overseeing is taking the operation against the coup plotters, CNN-Turk said.

The coup attempt began late Friday, with a statement from the military saying it had seized control "to reinstall the constitutional order, democracy, human rights and freedoms, to ensure that the rule of law once again reigns in the country, for law and order to be reinstated."

Fighter jets buzzed overhead, gunfire erupted outside military headquarters and vehicles blocked two major bridges in Istanbul. Soldiers backed by tanks blocked entry to Istanbul's airport for a couple of hours before being overtaken by pro-government crowds carrying Turkish flags, according to footage broadcast by the Dogan news agency.

But the military did not appear unified, as top commanders went on television to condemn the action and order troops back to their barracks.

Fighting continued into the early morning, with the sounds of huge blasts echoing across Istanbul and the capital, Ankara, including at least one bomb that hit the parliament complex. Television footage showed images of broken glass and other debris strewn across a lobby leading to the assembly hall.

In Washington, a statement from the White House said President Barack Obama and Secretary of State John Kerry agreed that both sides "should support the democratically elected government of Turkey, show restraint and avoid any violence or bloodshed."

A senior Defense Department official told Fox News that the unrest was having "no impact" on anti-ISIS missions flown out of Incirlik Air Base in southeastern Turkey.

NATO Secretary General Jens Stoltenberg issued a statement calling for "calm and restraint, and full respect for Turkey's democratic institutions and its constitution."

Leaders of Turkey's religious communities have released a joint statement condemning the attempted coup. The statement from representatives of the Jewish, Christian and Muslim communities declares "our great sorrow over the terrorist attacks that disturb the peace of our great nation and of the world."

Erdogan made his way to the Ataturk airport early Saturday and spoke to the crowds that gathered to greet him.

"They have pointed the people's guns against the people. The president, whom 52 percent of the people brought to power, is in charge. This government brought to power by the people is in charge. They won't succeed as long as we stand against them by risking everything."

Fox News’ Lucas Tomlinson and the Associated Press contributed to this report. A FoxNews.com correspondent in Istanbul contributed to this report