ISTANBUL — President Recep Tayyip Erdogan claimed victory on Sunday in a referendum that would give him vast new powers as the country’s unrivaled head of state, extending his influence over the judiciary and making him dominant over parliament.

But opposition parties quickly contested the result, demanding recounts of hundreds of thousands of ballots. Tallies published by the state news agency had showed the president’s supporters prevailing in the vote by a thin margin.

One of the opposition parties, the pro-Kurdish Peoples Democratic Party, or HDP, posted a message on Twitter saying it would appeal two-thirds of the votes.

The preliminary result, if confirmed, would cap a dramatic ascent for the populist Erdogan, a onetime mayor of Istanbul whose brand of Islamist and nationalist politics, and bareknuckle approach to his adversaries, have handed him and his party a string of election wins since 2002.

The president’s opponents have accused Erdogan of presiding over a stiflingly repressive state in recent years and said the constitutional changes, which will transform the system of government from a parliamentary to a presidential system, tilted the country toward the autocracies of the Middle East and away from Europe – delivering Turkey to one-man rule.

The arguments over the vote come at a time when Turkey’s Western allies have come to rely on Erdogan as a pivotal partner. This is especially true for the United States, which is leading a military coalition to defeat the Islamic State militant group across Turkey’s borders in Iraq and Syria. Turkey also hosts more than three million refugees and has struck a deal with European nations to prevent the refugees from traveling to their shores.

The changes would mean that Erdogan, who came to power as prime minister in 2003, will now be allowed to run for re-election in 2019 and serve two five-year terms – cementing, in the minds of many here, his status as the most consequential leader since Mustafa Kemal Ataturk, the founder of the Turkish Republic.

His supporters included his core constituency of conservative Muslims, who have delivered unflinching support to Erdogan for years as a champion of their concerns. He also successfully courted members of a Turkish nationalist party, who helped propel the constitutional amendments through the parliament.

During the campaign, Erdogan stoked a fight with several European allies, including Germany and the Netherlands. The cause, ostensibly, was a bar on Turkish officials campaigning for votes among Turkish expatriates in Europe. Erdogan used the squabble to maximum effect, deriding German and Dutch leaders as “Nazis,” in a series of broadsides that whipped up nationalist support at home.

There were questions about whether the referendum should have been held at all, with the country on a security footing and under a government-mandated state of emergency as a result of a failed coup last summer, and a number of other recent violent shocks.