By Tuvan Gumrukcu and Humeyra Pamuk
ANKARA/ISTANBUL (Reuters) - President Tayyip Erdogan declared victory in a referendum on Sunday to grant him sweeping powers in the biggest overhaul of modern Turkish politics, but opponents said the vote was marred by irregularities and they would challenge its result.
Turkey's mainly Kurdish southeast and its three main cities, including the capital Ankara and the largest city Istanbul, looked set to vote "No" after a bitter and divisive campaign.
Erdogan said 25 million people had supported the proposal, which will replace Turkey's parliamentary system with an all-powerful presidency and abolish the office of prime minister, giving the "Yes" camp 51.5 percent of the vote.
That appeared short of the decisive victory for which he and the ruling AK Party had aggressively campaigned. Nevertheless, thousands of flag-waving supporters rallied in Ankara and Istanbul in celebration.
"For the first time in the history of the Republic, we are changing our ruling system through civil politics," Erdogan said, referring to the military coups which marred Turkish politics for decades. "That is why it is very significant."
Under the changes, most of which will only come into effect after the next elections due in 2019, the president will appoint the cabinet and an undefined number of vice-presidents, and be able to select and remove senior civil servants without parliamentary approval.
Erdogan himself survived a failed coup attempt last July, responding with a crackdown that has seen 47,000 people detained and 120,000 sacked or suspended from their jobs.
In Ankara, where Prime Minister Binali Yildirim addressed cheering supporters, convoys of cars honking horns clogged a main avenue as they headed towards the AK Party's headquarters, their passengers waving flags from the windows.
But the head of the main opposition Republican People's Party (CHP), Kemal Kilicdaroglu, said the legitimacy of the referendum was open to question.
The party earlier said it would demand a recount of up to 60 percent of the votes after Turkey's High Electoral Board (YSK) announced it would count ballots which had not been stamped by its officials as valid unless they could be proved fraudulent.
Kilicdaroglu has accused Erdogan of seeking a "one-man regime", and said the proposed changes would put the country in danger.
In some affluent neighborhoods in Istanbul, people took to the streets in protest while others banged pots and pans at home - a sign of dissent that was widespread during anti-Erdogan protests in 2013.
In Istanbul's Besiktas neighborhood, more than 300 protesters brought traffic on a main street to a standstill, a Reuters cameraman at the scene said. In Ankara, scuffles between AK Party and opposition supporters broke out near the headquarters of the CHP.
Turkey's lira firmed to 3.65 to the dollar in Asian trade following the referendum, from 3.72 on Friday.
European politicians, however, who have had increasingly strained relations with Turkey, expressed concern. The European Commission, the executive body of the European Union, said the close result meant that Ankara should seek "the broadest national consensus" in implementing the vote.
Relations hit a low during the referendum campaign when EU countries, including Germany and the Netherlands, barred Turkish ministers from holding rallies in support of the changes.
Erdogan called the moves "Nazi acts" and said Turkey could reconsider ties with the European Union after many years of seeking EU membership.
Former Belgian prime minister Guy Verhofstadt, who heads the liberal group of MEPs in the European Parliament, said Erdogan needed to change course, noting the result was very tight. "If Erdogan persists, EU should stop accession talks," he said.
Manfred Weber, leader of the center-right grouping tweeted: "No matter the result: with his referendum Pres. Erdogan is splitting his country."
After the vote Erdogan repeated his intention to review Turkey's suspension of the death penalty, a step which would almost certainly spell the end of Ankara's EU accession process.
Further deterioration in relations with the European Union could also jeopardize last year's deal under which Turkey has curbed the flow of migrants - mainly refugees from wars in Syria and Iraq - into the bloc.
The referendum has bitterly divided the nation. Erdogan and his supporters say the changes are needed to amend the current constitution, written by generals following a 1980 military coup, confront the security and political challenges Turkey faces, and avoid the fragile coalition governments of the past.
"This is our opportunity to take back control of our country," said self-employed Bayram Seker, 42, after voting "Yes" in Istanbul.
"I don't think one-man rule is such a scary thing. Turkey has been ruled in the past by one man," he said, referring to modern Turkey's founder Mustafa Kemal Ataturk.
Opponents say it is a step towards greater authoritarianism. Erdogan and the AK Party enjoyed a disproportionate share of media coverage in the buildup to the vote while the leaders of the pro-Kurdish People's Democratic Party (HDP), which opposes the changes, have been in jail for months.
"I voted 'No' because I don't want this whole country and its legislative, executive and judiciary ruled by one man," said Hamit Yaz, 34, a ship's captain, after voting in Istanbul.
Proponents of the reform argue that it would end the current "two-headed system" in which both the president and parliament are directly elected, a situation they argue could lead to deadlock. Until 2014, presidents were chosen by parliament.
The government says Turkey, faced with conflict to the south in Syria and Iraq, and a security threat from Islamic State and PKK militants, needs strong and clear leadership.
The package of 18 amendments would give the president the authority to draft the budget, declare a state of emergency and issue decrees overseeing ministries without parliamentary approval.
(Additional reporting by Nick Tattersall, Ece Toksabay, Gulsen Solaker, Tulay Karadeniz, Orhan Coskun, Ercan Gurses in Ankara; Isabel Coles, Can Sezer, Birsen Altayli, Behiye Selin Taner, Ceyda Caglayan, Ebru Tuncay and Akin Aytekin in Istanbul, Philip Blenkinsop in Brussels; Writing by Dominic Evans and Daren Butler; Editing by Keith Weir, Adrian Croft and Bill Rigby)