* Results trickle in after nationwide municipal elections
* Eight killed in clashes in southeast
* Test of Erdogan's popularity after scandals and leaks (Adds initial results)
By Ayla Jean Yackley and Humeyra Pamuk
ISTANBUL/ANKARA, March 30 (Reuters) - Prime Minister Tayyip Erdogan said Sunday's bitterly contested local elections would affirm his legitimacy in battling graft allegations and security leaks he blames on "traitors" within the Turkish state.
The municipal elections have become a crisis referendum on Erdogan and his Islamist-rooted AK Party after weeks of scandal he has cast as a "dirty campaign" of espionage to implicate him in corruption and topple him after more than a decade in power.
Early results broadcast on Turkish television, with less than a fifth of votes counted, put the AKP ahead with between 44 and 48 percent of the vote. AKP needs to exceed its 2009 result of 38.8 percent to assert Erdogan's authority for a power struggle certain to continue after the polls.
Istanbul and Ankara, the two biggest cities, are expected to be particularly close. Two buses of riot police stood ready in Istanbul's plush Nisantasi neighbourhood, one of several districts rocked by anti-government protests last summer.
"Once the ballot boxes are opened, the rest is only footnotes to history," Erdogan said as he voted in Istanbul.
"Today it is what the people say which matters rather than what was said in the city squares," he told reporters, as supporters chanted "Turkey is proud of you" outside.
Erdogan has purged thousands of police and hundreds of judges and prosecutors since anti-graft raids in December targeting businessmen close to him and sons of ministers. He blames the raids on U.S.-based Islamic cleric Fethullah Gulen, an ex-ally who he says is using supporters in the police to try to topple the government.
"They are all traitors," Erdogan said of his opponents at a rally in Istanbul on the eve of the vote. "Let them do what they want. Go to the ballot box tomorrow and teach all of them a lesson ... Let's give them an Ottoman slap."
Voting passed off peacefully in most parts of the country, although clashes over local council positions killed eight in two separate shoot-outs in villages in the southeastern provinces of Hatay and Sanliurfa near the Syrian border.
Erdogan has crisscrossed the nation of 77 million during weeks of hectic campaigning to rally his conservative core voters, in a measure of how seriously he takes his party's first test at the ballot box since last summer's protests across the country and the eruption of the corruption scandal in December.
The level of support will be crucial for the future cohesion of his party - giving a measure of the effect, if any, the graft scandal has had on his popularity - and will influence his own decision on whether to run for the presidency in August.
A vote of less than 36 percent, considered unlikely, would be a huge blow for Erdogan and unleash AKP power struggles. A vote above 45 percent, some fear, could feed his authoritarian instincts, which have already seen bans on Twitter and YouTube in recent days, and herald a period of harsh reckoning with opponents in politics and state bodies.
The AKP's main opposition, the Republican People's Party (CHP), portrays Erdogan as a corrupt dictator ready to hang on to power by any means. Capture of the capital Ankara or Istanbul would allow them to claim some form of victory.
"These were supposed to be local elections but in the last few weeks they've sort of become general elections," said Efe Yamac Yarbasi, a teaching assistant voting in Ankara.
"I think Erdogan will remain the most powerful politician, but he will be damaged. Up until now each time his percentage has been rising, but from today, I think it will start to fall."
Erdogan formed AK in 2001, attracting nationalists and centre-right economic reformers as well as religious conservatives who form his base. Since his 2011 poll victory he has in his statements moved more towards these core supporters.
The graft scandal, also involving anonymous Internet postings of tapped state communications implicating Erdogan in corrupt actions he denies, was all but eclipsed in recent days by the leaking of a recording of a top-level security meeting.
In the recording, the intelligence chief, foreign minister and military commanders discuss possible armed intervention in neighbouring Syria's civil war. A senior government official described the leak as one of the biggest crises in modern Turkey's history, threatening further sensitive disclosures.
Officials suggest Gulen's Hizmet network released the recording, giving an alarming sense in Ankara that government has only tentative control of state bodies and part of the security apparatus while power struggles play out.
Hizmet denies orchestrating the leak scandal and manoeuvring to control the state apparatus, but those close to the network say they fear a heavy crackdown after the elections. (Additional reporting by Seyhmus Cakan in Diyarbakir, Dasha Afanasieva in Hatay, Daren Butler, Seda Sezer, Ece Toksabay, Can Sezer and Alexandra Hudson in Istanbul and Jonny Hogg and Gulsen Solaker in Ankara; Writing by Nick Tattersall; Editing by Ralph Boulton)