Erdogan, who has been the principal force in Turkish politics for more than a decade, had been heavily favored to triumph in the presidential race despite missteps over the past year or more that have alienated many voters outside of his religiously conservative base. He faced two far lesser-known opponents.
The 60-year-old leader, in his third term as prime minister, would have been forced by party rules to step down at the end of that tenure had he not opted to instead seek a five-year presidential term. If he has indeed won, he would be the longest-serving leader of the country since the father of the modern Turkish state, Mustafa Kemal Ataturk.
Erdogan's Islamist-leaning Justice and Development Party, or AKP as it is known by its Turkish initials, was hit earlier this year by a still-swirling corruption scandal, and he has angered Turkey's more liberal elements with authoritarian measures such as his harsh crackdowns on demonstrators and heavy-handed efforts to curtail use of social media. Compounding that was a seemingly unfeeling response in May to a disaster that killed more than 300 miners.
But over the past decade, Erdogan has also put Turkey on the economic map, presiding over an unprecedented period of growth. To entrepreneurial-minded followers he empowered, that trumps any failings.
In the tradition of Ataturk, Erdogan has sought to make himself an emblem of nationalistic pride. But secularists are wary of Erdogan's increasingly strident brand of Islamism, fearing it impinges on Turkey's historic separation of mosque and state.
Those who support him tend to be vociferous in their backing. "Strong, strong, who else is so strong?" said Mehmet Demir, who owns a small clothing business in the conservative Istanbul district of Fatih. "No one!"
After the polls closed, some young secular Turks confessed they were ambivalent about whether to even cast a vote.
"I feel he has a very different vision than mine of the kind of life that women should aspire to," said a graduate student named Zehra, who did not want her last name used because she dreaded confrontation with family members who back the prime minister. "But I don't know if I can change things here, or if I should just try to find a way to live abroad."
The prime minister's popularity is rock-solid among the more pious in his constituency, and many Turks have also applauded his excoriating criticism of Israel over Palestinian civilian deaths in the month-long war with militants in the Gaza Strip.
And a chilly turn in his dealings with Washington has probably helped him more than it has hurt him with his domestic constituency. Last month, he told a pro-government TV channel that he and President Obama don't talk on the phone any more.
To avoid a runoff later this month, Erdogan would have to have won an absolute majority on Sunday. Even if he has to face a second contest, most analysts believe he will ultimately prevail.
One of Erdogan's opponents, academic Ekmeleddin Ihsanoglu, complained of irregularities in the early hours of the vote, Turkish media reported. Throughout the campaign season, the prime minister's political opponents have accused him of using state institutions to advance his candidacy.
In the course of the campaign, enormous crowds regularly turned out for rallies that already had the trappings of a victory party. Erdogan has said that if he wins, he intends to make the presidency more powerful, stressing that Turkey's constitution does not prohibit him from doing so.