Erdogan, 69, had already overcome jail, mass protests and a bloody 2016 coup attempt to emerge as Turkey's most important leader in generations, but this year's election seemed one hurdle too many even for him.
An unprecedented opposition coalition, galvanised by a biting economic crisis and anger over a devastating February earthquake, had high hopes of ousting him from power on Sunday.
It failed at one of Erdogan's weakest moments, leaving him in prime position to extend his two-decade rule for one last time until 2028 on May 28.
A transformative and divisive figure, Erdogan is revered for unshackling religious restrictions in the officially secular but mostly Muslim state, overseeing ambitious infrastructure projects and turning Turkey into a geopolitical powerhouse.
Detractors accuse him of slipping into authoritarianism reminiscent of the Ottoman sultans, shaking Turkey's democratic foundations and impoverishing millions through misguided beliefs that contradict conventional economics.
Three months before the May 14 polls, a 7.8-magnitude earthquake that killed more than 50,000 people left Erdogan's government looking vulnerable as Turks vented fury at the state's slow response.
But not even that catastrophe stopped voters from giving him 49.5 percent of the first-round ballots.
Rewriting The Rules
Known to his inner circle as "beyefendi" (sir) and to admirers as "reis" (the chief), Erdogan prides himself on being able to woo doubters through tireless campaigning.
This passion has helped him and his party win more than a dozen local and national elections, allowing Erdogan to claim a people's mandate for his foreign military adventures and domestic clampdowns on dissent.
Erdogan risked his political domination on a 2017 referendum on abolishing the office of prime minister and handing greater powers to the president.
Erdogan eked out a narrow win, enfeebling parliament and enabling him to effectively rule by decree. It also gave him the constitutional loophole needed to run for two more terms in office -- an outcome acquiring the air of inevitability.
Power And Protests
Born in a working-class harbour district of Istanbul, Erdogan made his name in nascent Islamic movements that were challenging secular domination, becoming the city's mayor in 1994.
His term in office was cut short when he was convicted and jailed for four months for inciting religious hatred when he recited a fiery poem that compared mosques to army barracks and called minarets "our bayonets".
Among supporters, this only seemed to magnify his appeal.
Founding the AKP after an earlier Islamic party was banned, Erdogan spearheaded its 2002 landslide election victory and became premier less than six months later.
Erdogan's signature early achievements included a series of reforms that gladdened the European Union, including abolishing the death penalty and beginning a peace process with Kurdish militants.
Mass protests in 2013 over plans to turn an Istanbul park into a shopping mall marked the start of a more divisive era that included corruption allegations levelled against his inner circle.
Turkey's bid to join the EU faltered and peace talks with the Kurds imploded in 2015, when a Kurdish opposition party helped to wrest control of parliament from Erdogan for the first time.
Turkish military operations against Kurdish forces in Syria and Iraq soon followed, creating new tensions with the West.
Abandoned By Allies
In its early days the AKP, lacking allies and experience, forged an alliance with Islamic preacher Fethullah Gulen, who moved to permanent US exile in 1999 but retained strong influence in Turkish society and government.
Erdogan blamed Gulen for masterminding a bloody July 15, 2016, coup bid by a renegade army faction, charges he denies.
The president, on holiday in the Aegean, appeared on the FaceTime app on live TV to urge supporters out onto the streets.
It became the defining moment of his subsequent rule.
Erdogan fought back with sweeping purges that led to 80,000 arrests, brought most media under government influence and created a sense of looming peril among those opposed to his rule.
Washington and the EU's hesitation to openly support Erdogan in the first hours of the coup bid eroded his trust in the West and created diplomatic frictions that linger to this day.
Unorthodox economic policies, including lowering interest rates to fight soaring inflation that officially reached 85 percent in 2022, hurt many ordinary Turks as the lira slumped and bills spiked.
Opponents accused him of turning a blind eye to corruption and lax building standards that allowed entire cities to crumble in this year's earthquake.
Despite the widespread loss and anger, Erdogan's popularity on Sunday held up in parts of southeastern Turkey laid to waste by the disaster, a testament to a loyal bedrock of support built up over two decades.
Short link: