ANKARA, Turkey—The party of President Recep Tayyip Erdogan appeared to have lost the mayoral race in its historical bastion, Istanbul, a stinging setback for a leader who has towered over Turkish politics for the past 16 years.

The municipal votes, widely viewed as a referendum on Mr. Erdogan’s economic policies amid high inflation and slowing growth, sent the Turkish lira on a roller coaster. The currency, which shed nearly 30% against the dollar last year, skidded nearly 2% early Monday but recovered to gain nearly 2% in late European trading.

Preliminary results showed a dramatic loss of steam for Mr. Erdogan’s ruling Justice and Development Party, or AKP. Although the national election board stopped short of announcing a winner for Istanbul, it said the AKP was nearly 28,000 votes behind the rival Republican People’s Party, or CHP, in the megalopolis, where some nine million took part in the ballot.

Meanwhile, the CHP candidate in Istanbul, Ekrem Imamoglu, celebrated victory.

“The winners are 16 million Istanbulites,” he said on Monday. “We want to start serving the city as soon as possible.”