Turkish president Recep Tayyip Erdogan on Monday emerged from his toughest election test unbowed and in strong position to extend two decades of his Islamic-rooted rule by another five years in a historic 28 May runoff.
The 69-year-old leader defied pollsters and his country's most dire economic crisis since the 1990s to come within a fraction of a percentage point of winning Sunday's presidential ballot.
His right-wing party also retained control of parliament through an alliance with ultra-nationalists on a drama-filled night that concluded with Erdogan delivering a victory speech from a balcony to jubilant supporters.
He even won in regions hit by a calamitous February earthquake that claimed more than 50,000 lives -- and where anger at the government's slow response to Turkey's worst disaster of modern times was seething.
"A staggering win for Erdogan," emerging markets economist Timothy Ash said in a note to clients.
"He has the magic dust at these times. And he just gets Turks -- the nationalist, socially conservative and Muslim ones."
The main opposition party led by Kemal Kilicdaroglu confronted the reality Monday that they were unable to beat Erdogan at one of his most vulnerable moments.
"Don't despair," Kilicdaroglu told his supporters. "We will stand up and take this election together."
Turkey's election officials confirmed that there would be a second round because the remaining uncounted votes would not swing the outcome.
Erdogan secured 49.5 percent of the vote and Kilicdaroglu picked up 44.9 percent.
Nationalist candidate Sinan Ogan -- a former member of a far-right party now allied with the government -- won 5.2 per cent.
Official turnout reached a record 88.9 per cent.
Observers from the Council of Europe said the election was "marked by an unlevel playing field but still competitive".
The markets were depressed and Erdogan's supporters ecstatic.
The lira touched new lows against the dollar and stocks on the Istanbul exchange fell on a realisation that the era of Erdogan's unconventional economics may not be over.
"We think Turkey is now at very high risk of an increase in macroeconomic instability," the Capital Economics consultancy said.
The view was different in the more nationalist and conservative corners of Turkey.
"The people won!" the right-wing Yeni Safak newspaper proclaimed in a banner headline.
The pro-government Sabah daily called Erdogan's performance a "superb success".
Erdogan supporter Hamdi Kurumahmut was brimming with confidence the morning after Turkey's biggest election of its post-Ottoman era.
"Erdogan is going to win. He is a real leader. The Turkish people trust him. He has a vision for Turkey," Kurumahmut told AFP in Istanbul.
US president Joe Biden is "looking forward to working with whoever" wins Turkey's knife-edge election, the White House said Monday as it praised the NATO ally for holding a peaceful vote.
"There are things that need to be improved on the economy, education or the refugee policy. But we know he's the one who can sort all that out," the 40-year-old tourism sector worker added.
US president Joe Biden is "looking forward to working with whoever" wins Turkey's knife-edge election, the White House said Monday as it praised the NATO ally for holding a peaceful vote.
"We congratulate the Turkish people for expressing their desires at the ballot box in a peaceful way," National Security Council spokesman John Kirby told reporters.
Some Kilicdaroglu supporters tried to stay positive.
"I don't want to even think about a scenario in which Erdogan wins," Emin Serbest said as the last voted were being counted.
"If Kilicdaroglu wins... a beautiful time awaits us," said the 33-year-old Istanbul municipality worker.
But most analysts feel that Kilicdaroglu and his six-party opposition alliance will have a difficult time halting Erdogan's momentum over the coming two weeks.
Emre Peker of the Eurasia Group consultancy put the odds of an Erdogan victory at 80 per cent.
"The results show that Erdogan and his allies successfully bolstered the incumbent's support with strong messaging on terrorism, security, and family values -- even as the economy continued to top voter concerns," Peker said in note.
Political risk consultant Anthony Skinner said Sunday's result underscored the difficulty of trying to gauge public opinion in the strongly polarised nation of 85 million people.
"Many pre-election public opinion poll results did not reflect Erdogan's resourcefulness and the degree of support he still enjoys in the country," the veteran Turkey watcher said.
"It just goes to show how careful one needs to be when looking at public opinion polls prior to elections."