ISTANBUL — A military coup attempt plunged Turkey into a long night of violence and intrigue on Friday, threatening its embattled president, leaving nearly 200 dead and injecting new instability into a crucial NATO member and American ally in the chaotic Middle East.

The coup attempt was followed hours later by an equally dramatic public appearance by the president, Recep Tayyip Erdogan, whose whereabouts had been unknown for hours after the plotters claimed to have taken control. Flying into Istanbul Ataturk Airport from an undisclosed location early Saturday, Mr. Erdogan signaled that the coup was failing.

“A minority within the armed forces has unfortunately been unable to stomach Turkey’s unity,” Mr. Erdogan said after the private television channel NTV showed him greeting supporters. Blaming political enemies, Mr. Erdogan said: “What is being perpetrated is a rebellion and a treason. They will pay a heavy price for their treason to Turkey.”

Mr. Erdogan suggested that the plotters had tried to assassinate him, referring to a bombing in the Turkish Mediterranean resort town of Marmaris after he left on Friday. “It would appear that they thought I was there,” he said.