ANKARA, Turkey — Turkey’s presidential election appeared on Sunday to be headed for a runoff after the incumbent, Recep Tayyip Erdogan, failed to win a majority of the vote, a result that left the longtime leader struggling to stave off the toughest political challenge of his career.

The outcome of the vote set the stage for a two-week battle between Mr. Erdogan and Kemal Kilicdaroglu, the opposition leader, to secure victory in a May 28 runoff that may reshape Turkey’s political landscape.

With the unofficial count nearly completed, Mr. Erdogan received 49.4 percent of the vote to Mr. Kilicdaroglu’s 44.8 percent, according to the state-run Anadolu news agency.

But both sides claimed to be ahead.

“Although the final results are not in yet, we are leading by far,” Mr. Erdogan told supporters gathered outside his party’s headquarters in Ankara, the capital.

Speaking at his own party’s headquarters, Mr. Kilicdaroglu said the vote would express the “nation’s will.” He said, “We are here until each and every vote is counted.’’

The competing claims came early Monday after a nail-biter evening during which each camp accused the other of announcing misleading information. Mr. Erdogan warned the opposition on Twitter against “usurping the national will” and called on his party faithful “not to leave the polling stations, no matter what, until the results are finalized.”

Opposition politicians disputed the preliminary totals reported by Anadolu, saying that their own figures collected directly from polling stations showed Mr. Kilicdaroglu in the lead.

At stake is the course of a NATO member that has managed to unsettle many of its Western allies by maintaining warm ties with the Kremlin. One of the world’s 20 largest economies, Turkey has an array of political and economic ties that span Asia, Africa, Europe and the Middle East, and its domestic and foreign policies could shift profoundly depending on who wins.

Supporters of President Recep Tayyip Erdogan in Istanbul on Sunday night.Credit…Ozan Kose/Agence France-Presse — Getty Images

The vote was in many ways a referendum on the performance of Mr. Erdogan, Turkey’s dominant politician for 20 years.

After he became prime minister in 2003, he presided over a period of tremendous economic growth that transformed Turkish cities and lifted millions of Turks out of poverty. Internationally, he was hailed as a new model of a democratic Islamist, one who was pro-business and wanted strong ties with the West.

But over the past decade, Mr. Erdogan’s critics grew both at home and abroad. He faced mass protests against his governing style in 2013, and in 2016, two years after he became president, he survived a coup attempt. Along the way, he seized opportunities to sideline rivals and gather more power into his hands, drawing accusations from the political opposition that he was tipping the country into autocracy.

Since 2018, a sinking currency and inflation that official figures say exceeded 80 percent last year and was 44 percent last month have eroded the value of Turks’ savings and salaries.

Mr. Erdogan’s inability to clinch a victory in the first round of voting on Sunday confirmed a decline in his standing among voters angry with his stewardship of the economy and his consolidation of power. In his last election, in 2018, he won outright against three other candidates with 53 percent of the vote. His closest challenger received 31 percent.

On Sunday, one voter, Fatma Cay, said she had supported Mr. Erdogan in the past but did not do so this time, in part because she was angry at how expensive foodstuffs like onions had become.

“He has forgotten where he comes from,” said Ms. Cay, 70. “This nation can raise someone up, but we also know how to bring someone down.”

Still, she did not flip to Mr. Kilicdaroglu, voting instead for a third candidate, Sinan Ogan, who received about 5 percent of the vote. The elimination of Mr. Ogan could give an edge to Mr. Erdogan in the runoff, as Mr. Ogan’s right-wing nationalist followers are more likely to prefer him.

Mr. Erdogan remains popular with rural, working class and religious voters, who credit him with developing the country, enhancing its international standing and expanding the rights of devout Muslims in Turkey’s staunchly secular state.

“We just love Erdogan,” said Halil Karaaslan, a retiree. “He has built everything: roads, bridges and drones. People are comfortable and in peace.”

That, Mr. Karaaslan said, was more important than rising prices. “There is no economic crisis,” he said. “Sure, things are expensive, but salaries are almost as high. It balances.”

Seeking to capitalize on voter frustration, a coalition of six opposition parties came together to challenge Mr. Erdogan, backing a joint candidate, Mr. Kilicdaroglu.

Kemal Kilicdaroglu represented several opposition parties to mount a challenge to Mr. Erdogan.Credit…Bulent Kilic/Agence France-Presse — Getty Images

Mr. Kilicdaroglu, a former civil servant who ran Turkey’s social security administration before leading Turkey’s largest opposition party, campaigned as the antithesis of Mr. Erdogan. Offering a contrast to Mr. Erdogan’s tough-guy rhetoric, Mr. Kilicdaroglu filmed campaign videos in his modest kitchen, talking about daily issues like the price of onions.

Sunday’s vote was also held to determine the makeup of Turkey’s 600-member Parliament, although the results for those seats were not expected until Monday. The Parliament lost significant power when the country changed to a presidential system after a referendum backed by Mr. Erdogan in 2017. The opposition has vowed to return the country to a parliamentary system.

Adding to the importance of these elections for many Turks is that 2023 marks the 100th anniversary of the country’s founding as a republic after the collapse of the Ottoman Empire. A national celebration is scheduled for the anniversary, on Oct. 29, and the president will preside over it.

The election was also driven by issues that have long polarized Turkish society, like the proper place for religion in a state committed to strict secularism. In his 11 years as prime minister and nine as president, Mr. Erdogan has expanded religious education and eased rules that restricted religious dress.

Derya Akca, 29, cited her desire to cover her hair as a primary reason she supported Mr. Erdogan’s Justice and Development Party. “They defend my freedom to wear a head scarf, which is the most important factor for me,” said Ms. Akca, who works in an Istanbul clothing store.

She recalled being so embarrassed after a college professor humiliated her in front of the class that she quit school, a decision she now regrets. “I felt like an outsider,” she said. “I now wish I had stayed and fought.”

But elsewhere in the city, Deniz Deniz, the co-owner of a bar popular with the city’s L.G.B.T.Q. community, bemoaned how the number of such establishments had diminished in the past decade of Mr. Erdogan’s tenure.

“I want so much to change,” they said. “I want a country where LGBT+ folk and women aren’t rejected. I want an egalitarian and democratic country.”

Turks casting ballots in Istanbul on Sunday.Credit…Sergey Ponomarev for The New York Times

In Turkey’s southern region, which was devastated by powerful earthquakes in February that killed more than 50,000 people, many voters took out their anger at the government’s response at the ballot box.

“We had an earthquake and the government didn’t even intervene,” said Rasim Dayanir, a quake survivor who voted for Mr. Kilicdaroglu. “But our minds were made up before the earthquake.”

Mr. Dayanir, 25, had fled the city of Antakya, which was largely destroyed in the quake, but returned with eight family members to vote on Sunday.

He stood amid hundreds of voters who had lined up to vote inside of a primary school. Others cast votes in shipping containers that had been set up to replace destroyed polling places. Mr. Dayanir said his uncle, aunt and other members of his family had been killed in the quake.

“We are hopeful,” he said. “We believe in change.”

Ben Hubbard reported from Ankara, and Gulsin Harman from Istanbul. Reporting was contributed by Elif Ince from Istanbul, Safak Timur from Ankara and Nimet Kirac from Antakya.