By Ercan Gurses and Orhan Coskun

ANKARA (Reuters) - Turkey's Islamist-rooted AK Party swept to an unexpected victory in elections on Sunday, returning the country to single-party rule in an outcome that will boost the power of President Tayyip Erdogan but may sharpen deep social divisions.

With almost all ballots counted, the AKP had taken just shy of 50 percent of the votes, comfortably enough to control a majority in the 550-seat parliament and a far higher margin of victory than even party insiders had expected.

Erdogan said the outcome was a vote for stability, and a message to Kurdish insurgents in the country's restive southeast that violence could not coexist with democracy.

Prime Minister and AKP leader Ahmet Davutoglu tweeted simply "Elhamdulillah" (Thanks be to god), before emerging from his family home in the central Anatolian city of Konya to briefly address crowds of cheering supporters.

"Today is a victory for our democracy and our people ... Hopefully we will serve you well for the next four years and stand in front of you once again in 2019," he said.

At AKP headquarters in Ankara, under a sky lit by fireworks, he later urged Turkey's political parties to work together on a new constitution, which Erdogan has said he would like to see include executive powers for the presidency.

A senior official from the main CHP opposition, which had calculated on 'reining in' Erdogan's influence with a coalition government, described the result as "simply a disaster".

The outcome could aggravate deep splits in Turkey between pious conservatives who champion Erdogan as a hero of the working class, and Western-facing secularists suspicious of his authoritarianism and Islamist ideals.

In the mainly Kurdish southeastern city of Diyarbakir, security forces fired tear gas at stone-throwing protesters after support for the pro-Kurdish opposition fell perilously close to the 10 percent threshold needed to enter parliament.

In June, the AKP lost the overall majority it had enjoyed since 2002. Erdogan had presented Sunday's polls as a chance to restore stability at a time of tension over Kurdish insurrection and after two bombings, attributed to Islamic State, while critics fear a drift to authoritarianism under the president.

Story continues

"The election results show that our nation has sided with looking after the environment of stability and trust that was risked on June 7," he said in a statement.

Since June's poll, a ceasefire with Kurdish militants has collapsed, the war in neighbouring Syria has worsened and Turkey - a NATO member state - has been buffeted by two Islamic State-linked suicide bomb attacks that killed more than 130 people.

Investors and Western allies hoped the vote would help restore stability and confidence in an $800 billion economy, allowing Ankara to play a more effective role in stemming a flood of refugees from nearby wars via Turkey into Europe and helping in the battle against Islamic State militants.

WAITING FOR SIGNS

With 99 percent of votes counted, the AKP was on 49.4 percent, according to state-run broadcaster TRT, giving it 316 of parliament's 550 seats. The main opposition CHP was at 25.4 percent.

The lira currency firmed to its strongest in 2-1/2 months on the results. Investors had been pricing in a coalition, but the prospect of a strong stable government - even a polarising one - appeared to offer relief after months of uncertainty.

Erdogan's crackdowns on media freedoms and tightening grip on the judiciary, following a corruption investigation that was shut down as an attempt to overthrow him, have alarmed European leaders. A large number of journalists and others have faced court proceedings for "insulting the president".

Foreign capitals as well as Turkish media and other organisations will be watching closely for signs of whether a harsh climate will continue or government relaxes its grip.

Erdogan and the AKP have been fierce critics, for example, of U.S. support for Kurdish militia fighters battling Islamic State (IS) across Turkey's border in Syria.

"This (result) makes more difficult a strategy of using the Kurds against IS because AKP appeals to anti-Kurd sentiments," said Bruce Riedel, a former CIA analyst and sometime policy advisor to U.S. President Barack Obama.

The pro-Kurdish HDP, which scaled back its election campaign after its supporters were targeted in the Ankara suicide bomb attack that killed more than 100 people on Oct. 10, was on 10.7 percent, according to TRT. It won 13 percent in June.

The nationalist MHP, which was another casualty of the rise in AKP support, saw its share of the vote drop to 12 percent from 16.5 percent in June.

The election was prompted by the AKP's inability to find a junior coalition partner after the June outcome. Erdogan's critics said it represented a gamble by the combative leader to win back enough support so the party can eventually change the constitution and give him greater presidential powers.

PRESIDENTIAL REPUBLIC

Erdogan, Turkey's most powerful leader in generations, resigned as prime minister last year and became Turkey's first directly elected president - with the aim of transforming it from a largely ceremonial position to a strong executive post.

The AKP still lacks a majority big enough to change the constitution. But being the sole party in power, Erdogan will be able to reassert his influence over government from the grandeur of his newly built presidential palace.

"Turkey lost considerable ground in economy, politics and terror during this period, and gains were lost. Voters appeared to want to bring back stability once again," a second AKP official said.

Some Western allies, foreign investors and Turks had seen an AKP coalition with the CHP as the best hope of easing sharp divisions in the EU-candidate nation, hoping it might keep Erdogan's authoritarian instincts in check.

"I've given up on the AKP. The honest party is the CHP. The country needs to heal its wounds," said Yasar, a 62-year-old retired labourer now working as a shoeshine man outside a mosque in the conservative Istanbul district of Uskudar.

But across the Bosphorus in the city's Tophane district, an AKP stronghold, teenagers with drums paraded in celebration. Cars honked their horns as passengers waved AKP flags.

"In June, people wanted to send a message to the AKP, but in fact the people got the message," said Osman Aras, 35, a food merchant. "Without the AKP this country will sink into chaos. We need a strong government to guide us through these times."

(Additional reporting by Daren Butler, Ayla Jean Yackley and Melih Aslan in Istanbul; Humeyra Pamuk in Diyarbakir; Ece Toksabay, Jonny Hogg, Tuvan Gumrukcu and Can Barut in Ankara, Mark Hosenball in Washington; Writing by Nick Tattersall and David Dolan; Editing by Ralph Boulton and Eric Walsh)