"I would like to announce to Istanbul's residents and all of Turkey that our numbers show that it is clear we won Istanbul", Imamoglu said in a speech in the early hours of Monday.
The opposition candidate for Istanbul mayor, Ekrem Imamoglu was leading by almost 28,000 votes with most ballot boxes counted, Supreme Election Board (YSK) chairman Sadi Guven said though final results would confirm the victor.
In the capital, Ankara, unofficial results showed that CHP candidate Mansur Yavas had garnered 50.9 percent, with the AK Party nominee Mehmet Ozhaseki trailing on 47.2 percent.
Neither Ankara nor Istanbul have been controlled by a party other than the AKP or its predecessor the Welfare Party (RP) since 1994.
While official vote tallies and Turkish broadcasters put the main opposition Republican People's Party (CHP) candidates ahead in both cities, the Islamist-rooted AKP promised appeals that could delay the final results for days.
Sezgin Tanrikulu, a CHP MP from Istanbul, said that although Imamoglu won the race in Istanbul, the election board was waiting for the objection period to end for legal reasons to declare the official victor.
"But we do take the fact that many parties have been successful as a positive sign of Turkey's democratic resilience", he told reporters in Ankara.
The election commission also said that the opposition party is taking the lead in Istanbul, the country's largest city. Losing Istanbul, a city three times the size of the capital, where he launched his political career and served as mayor in the 1990s, would be an even greater shock.
Ole Gunnar Solskjaer named Manchester United manager on three-year contract
He has performed the cultural reset, but as he admitted in his news conference Thursday, this is when the hard work really begins. I'm the same - I can't be complacent now I've got the job.
In Istanbul, the AKP's Binali Yildirim, a former prime minister and close ally of Erdogan, and the CHP's Ekrem Imamoglu both had a 48.7 percent share of the vote with 99 percent of ballots counted, but with Yildirim ahead by fewer than 5,000 out of more than eight million votes cast.
Supporters of opposition Republican People's Party (CHP) celebrate after early results of the local election in Ankara.
But the recent economic troubles took a toll on party support, two AK Party sources said, after the lira slumped against the dollar a year ago, inflation jumped to 20 percent, unemployment climbed and the economy tipped into recession.
Investors fear Erdogan's electoral losses will lead him "to be more defensive, trying to shore up electoral support via populist measures, which increases risks for markets", said Inan Demir, senior emerging market economist at Nomura, in London.
In February, inflation stood at just under 20 percent, while the Central Bank's main interest rate is now 24 percent.
At 1523 GMT, the lira stood at 5.45 against the USA dollar, firming from Friday's close of 5.5550. The HDP denies links to the outlawed militant Kurdistan Workers Party (PKK).
The Ankara mayoral post is now in opposition hands.
A European group observing Turkey's local elections criticised curbs on the free expression of citizens and journalists, saying on Monday it was unsure whether the Turkey electoral environment was free and fair.