By Ali Kucukgocmen and Tuvan Gumrukcu

ISTANBUL/ANKARA (Reuters) - Turkey's main opposition candidate took office as Istanbul mayor on Wednesday after a stunning victory over President Tayyip Erdogan's AK Party, but the new head of the country's largest city still faces an appeal for the vote to be re-run.

The final result of the March 31 local elections gave a wafer-thin majority to the secularist opposition Republican People's Party (CHP) in Turkey's commercial hub, ending 25 years of control by the AK Party (AKP) and its Islamist predecessors.

The loss is especially hard for Erdogan who launched his political career in Istanbul as mayor in the 1990s and has triumphed in more than a dozen parliamentary, presidential and local polls since his party came to power in 2002.

The Turkish lira, which has dipped since the election, firmed on Wednesday.

Addressing thousands of flag-waving supporters outside the municipality building, Ekrem Imamoglu promised to work for all 16 million residents.

"We never gave up, we never gave up on our battle for democracy and rights," he said. "We are aware of our responsibilities and the needs of this city. We will start to serve immediately."

Imamoglu's margin of victory - the final count put him about 13,000 votes, or less than 0.2 percentage points, ahead of the AKP candidate and former prime minister Binali Yildirim - prompted several AKP challenges.

On Tuesday, after 16 days of appeals and recounts, the AKP asked the High Election Board to annul and re-run the election in Istanbul over what it said were irregularities. Its nationalist MHP allies made a similar request on Wednesday.

But Yildirim struck a conciliatory note on Wednesday evening while stressing that the AKP had not abandoned its appeal.

"I hope the decision (to offer Imamoglu the mandate) will bring good. Democracy and law have worked and we see there is no need for raised tensions," he said on Twitter.

Imamoglu said he hoped the election board would deal with the AKP challenge in a "just way", adding he had won his mandate from the voters and only they could take it away.

Story continues

"TOO MANY IRREGULARITIES"

The repeated challenges by the AKP and MHP have fueled frustration among opposition supporters which spilled over into football stadiums at the weekend when fans chanted at top Istanbul derby matches for the mayoral mandate to be given to their candidate.

"There are way too many irregularities," AKP Deputy Chairman Ali Ihsan Yavuz said, presenting the party's justification for its demand for a new vote and complaining of "organized fraud".

CHP Deputy Chairman Muharrem Erkek responded that there were "no concrete documents, information or evidence in the AKP appeal for an annulment".

While the AKP was defeated in the battle for Istanbul mayor, results showed the party had won most seats in its municipal councils. The AKP's re-run appeal applies only to the mayoral elections, not those for municipal councils.

Wolfango Piccoli, co-president of Teneo political risk advisers, said it was puzzling to call only for a re-run of the mayoral elections, and added that some areas where the AKP claimed fraud took place were under its own rule.

Uncertainty over the results has put pressure on financial markets, pushing the lira down nearly 5 percent.

Erdogan had vowed that Turkey would enter a four and half year period with no elections after March 31, during which the ailing economy would be the focus. If the AKP appeal is upheld, Istanbul, which accounts for more than a third of Turkey's economy, would vote again on June 2.

(Additional reporting Murad Sezer in Istanbul and Ece Toksabay in Ankara; Editing by Dominic Evans and Gareth Jones)