Turkey's embattled prime minister, Recep Tayyip Erdogan, appeared to fight off serial allegations of sleaze on Sunday night when his governing Justice and Development party (AKP) gained a substantial victory in nationwide local elections seen as a barometer of the prime minister's popularity.

Inconclusive projections and partial ballot returns showed the AKP likely to hold Istanbul and reach its declared goal of more than 40% of overall votes.

Speaking at a victory rally in Ankara, Erdogan signalled a crackdown on opponents, especially the network of Islamic cleric Fethullah Gülen, his former ally: "There will not be a state within the state. We will root them out," he told the crowd.

He said it was time for a "new Turkey", and added: "Seventy-seven million should know that the new Turkey has won today. Today is a day of victory."

Confusion reigned during the counting of the votes, with hundreds of election-rigging allegations during the first hours after ballots closed. The AKP and the opposition CHP both jumped the gun to argue they had won the two main cities, increasing rather than easing the polarisation of recent months as Erdogan has opted for confrontation to fight off damaging allegations of sleaze and authoritarianism.

After his CHP rival, Mansur Yavas, declared victory in a press conference, Ankara incumbent Melih Gökcek followed suit, accusing his opponents of "manipulations" and promising supporters a "historic victory." Similar statements were later made for Turkey's biggest metropolis and economic centre, Istanbul, with AKP and CHP officials slamming local news agencies for "deliberate manipulations".

Trust in election results was at an all-time low as the state-run Anatolian news agency and the privately owned Cihan service published different numbers.

With about half of votes counted, results cited on Turkish television put Erdogan's Islamist-rooted party ahead with 44-46%, and the main opposition CHP on 23-28%. If that result is borne out, Erdogan may claim at least for now to have ridden out the biggest challenge to his 12-year rule.

He said the results would reaffirm his legitimacy after a turbulent election campaign that has seen his government tighten its grip on the courts, purge thousands of police, and block access to Twitter and YouTube as it tried to stem a flow of corruption allegations.

"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."

The races were close in the two biggest cities, Istanbul and Ankara, but the ruling party appeared to have the edge, with AKP supporters honking car horns in celebration in the capital. Two buses of riot police stood ready in Istanbul's plush Nisantasi neighbourhood, one of several districts rocked by anti-government protests last summer.

The AKP had set itself a goal of exceeding its 2009 result of 38.8%, and Erdogan is likely to assert his authority even more strongly in a power struggle continuing after the polls. A strong showing would embolden Erdogan to run for the presidency in an August election. Some fear it could also feed authoritarian tendencies and herald a period of harsh reckoning with opponents in politics and state bodies.

More than 92% of the 52 million-plus people eligible to vote cast their ballots in the first popular test for Erdogan since last summer's large anti-government protests and allegations of massive corruption inside the Turkish government.

Erdogan has responded ferociously to sleaze allegations that implicate his close family and high government officials, and purged the police and the judiciary of thousands of critics.

Although not standing for election, Erdogan has campaigned tirelessly in support of AKP candidates.

Speaking at a rally in Istanbul on Saturday, he lashed out at his political opponents. "They are all traitors," he told a cheering crowd. "Go to the ballot box tomorrow and teach all of them a lesson. Let's give them an Ottoman slap."

At a polling station in a lower-middle-class neighbourhood in Istanbul on Sunday, businessman Yusuf Dindarol, 51, said his reasons to vote for the AKP were less pugnacious. "I want stability and quiet to return to Turkey. I haven't been able to do any business since the scandals in December," he explained.

"We have tried 80 years of the CHP, and they were unable to give us the prosperity that the AKP has given us.

"If [the CHP] wins, the country will fall apart. I need to think economically, and there simply is no alternative to the AKP."

Despite its tarnished image, the AKP was widely expected to win the most votes. In 2009, during the last local polls, the party achieved 39% of the vote.

Ebru, 19, casting her vote for the first time, said she did not believe the corruption allegations. "I only believe what I see with my own eyes," she said. "What counts for me is the service they provide. I work very far from my house, and with the new metro line, it only takes me 30 minutes to get there."

Her parents, who also both cast their votes for the AKP, agreed. "I fully trust the prime minister and the AK party to continue their good work," said Ebru's mother.

Others disagreed. "I used to vote for the AKP, because they did a good job," said Nurgül, 25, a banker. "But since the Gezi protests [last summer], my trust in them has waned. When they closed down Twitter, I decided that my vote would go to the CHP this year."

For many, their trust in fair elections in Turkey has been severely shaken, and Erdogan's divide-and-rule style to rally his religious-conservative base has led to increasing polarisation of the country, and in some cases to violence.

In the runup to the local elections, local offices of the CHP and the pro-Kurdish HDP have repeatedly been attacked. Analysts have criticised the amount of time given to the AKP on state television in comparison with its opponents, and allegations of attempts at election-rigging have circulated on social media.

Voting went ahead peacefully in most parts of the country , but fights broke out between groups supporting rival candidates in two villages near the south-eastern border with Syria. Six people were killed in a shoot-out in Sanliurfa province, while two more died in a village in Hatay, security officials said. The clashes were over local council positions and were not directly linked to the wider tensions in the country.

At a polling station in Okmeydani, dozens of volunteer monitors stood watch at ballot boxes.

"I am here to make sure everything is done right," said lawyer Cem Yanki, 25, a volunteer for the civil rights platform Öy ve Ötesi (Ballot and Beyond). "This is the first time we have such an initiative in Turkey. Why not make use of our democratic rights?"

Ayse, 60, a volunteer at a polling station in the Istanbul district of Alibeyköy, said civil rights movements like Öy ve Ötesi gave her hope: "I came here to support my daughter and her future," she explained. "People realise that democracy requires work. We used to be lazy and uninterested, and we saw what happened then. We are to blame for that. But now we know that every vote counts, that we have to be vigilant."