The AKP was expected to fall far short of the 330 seats needed to force a national referendum on Erdogan's plan to scrap Turkey's parliamentary structure for a presidential system, with him at the top.
"The nation's decision is the best decision. Do not worry," said Prime Minister Ahmet Davutoglu in a cagey post-election speech. Davutoglu was the face of the AKP's election campaign, if not necessarily the figure pulling its strings.
The deciding factor in this election was the emergence of the Peoples' Democratic Party, or HDP, which came in fourth with 13 per cent of the vote. For a political party to enter Turkey's Parliament, it has to pass a high threshold of 10 per cent of the total vote. The HDP did so, and will now command 82 seats in the 550-seat legislature, mostly at the expense of the AKP.
It's a remarkable achievement for a party that was formed less than three years ago and has direct ties to the violent three-decade-old Kurdish separatist insurgency in Turkey's southeast. The war between the PKK, as the Kurdish militant group is known, and the Turkish state had claimed 40,000 lives since the early 1980s.
"From now on, HDP is Turkey's party. HDP is Turkey, Turkey is HDP," said the party's leader, Selahattin Demirtas, at a press conference in Istanbul after the voting.
The party framed itself as a leftist movement for all Turks, and boasted a diverse slate of parliamentary candidates. These included representatives from virtually all of Turkey's major ethnic groups, a large number of women and the nation's first openly gay candidate.
Their progressive agenda appealed to disgruntled urban voters who disliked the AKP and also struggled to identify with the other main opposition parties - the secularist CHP, a staunchly republican party that walks in the shadow of Turkey's founding father, Mustapha Kemal Ataturk, and the ultra-nationalist MHP, which is "a stone's throw away from fascist", says Aaron Stein, an associate fellow at London's RUSI think tank.
"The HDP represents the diversity of the minorities in Turkey, and a real pluralist democracy," says Laura Batalla, Secretary General of Friends of Turkey, a unit attached to the European Parliament.
The HDP's success, given its origins, speaks to the maturing of Turkish democracy. Among the HDP's parliamentarians are the relatives of jailed or slain leaders of the PKK, an organisation still considered a terrorist group by Washington and Ankara. Demirtas, the HDP's charismatic icon, is believed to have a brother among the guerrillas.
For decades, Kurds and Kurdish identity were suppressed by the Turkish state. Ironically, it was under Erdogan and the AKP that Kurds began to experience more cultural rights.
It's now "impossible to sideline Kurdish politics", says Akin Unver, a professor of international relations at Kadir Has University in Istanbul.
"Despite the civil war of the 1990s, Kurds have evolved politically and established a lasting legacy."
It's unclear what may follow. The three main opposition parties have ruled out entering into a coalition with the AKP, which may choose to attempt a minority government or enter a scenario where early elections are called.
Analysts believe Erdogan's desire to assume further powers and eliminate checks through an executive presidency badly backfired.
"Erdogan may have made the mistake of his political life by going for the presidency," says Aliriza, instead of remaining as prime minister, a post he held from 2003 until last year. "If he had not decided to go for the presidency, we would now be discussing his record fourth electoral victory as prime minister."
Turkey under his rule since 2002 has transformed dramatically. Reforms have lifted a new middle class and empowered a whole strata of society outside the secular elites entrenched in the western coastal cities.
In the Istanbul neighbourhood of Kasimpasa, a traditionally lower-middle-class area and AKP stronghold where Erdogan was born, voters praised his years of rule.
"Thanks to Erdogan, so much has changed. There is prosperity and stability," said Hafiza Aktas, who cast her ballot at a polling station in the primary school where Erdogan was once a student.
Aktas said the AKP's sweeping reforms of Turkey's health system had been life-saving for her family. And, pointing to her red-and-black headscarf, she said the party had lifted decades of secularist suppression of the country's devout Muslims.
"I can go everywhere now. I have real freedom," said Aktas, referring to earlier bans on the wearing of headscarves at public institutions.
But the AKP has been hobbled by corruption scandals and anger about its increasing control over state institutions, as well as fatigue with Erdogan's authoritarian style. It lost considerable votes in central and eastern Anatolia, usually a reliable base of support, both to the far-right MHP and the leftist HDP.
HDP leader Demirtas cast his ballot at a school in Sultanbeyli, a working-class neighbourhood in Istanbul with a large Kurdish population. On its approach, the car carrying Demirtas was mobbed by supporters. Youth, some decked out in shirts and flags hailing Kurdistan, clambered on fences and walls to catch a glimpse of their hero.
"Our leader is the greatest leader," they chanted.
Vote a boost for Kurds
With fireworks and gunshots, Turkey's Kurds have celebrated their biggest-ever breakthrough in Turkish politics, revelling in the blow dealt to President Recep Tayyip Erdogan. Cars cruised through the streets of the Kurdish-majority city of Diyarbakir in southeastern Turkey, with people hanging out of the windows making "V" signs.
Results showed the pro-Kurdish People's Democratic Party (HDP) and its charismatic leader Selahattin Demirtas easily passed the 10 per cent barrier needed to win seats in Parliament. The results will give the Kurds - who, with 20 per cent of Turkey's population, are the country's biggest minority - true representation in Parliament. In Diyarbakir, supporters were ululating, dancing and shouting the party's election slogan: "We are the HDP, we are going to the Parliament."
One Kurd said Diyarbakir had last "seen such a night of celebration during the liberation of Kobane".
Kurdish fighters, backed by US-led air strikes, drove Isis (Islamic State) jihadists from the key Syrian town of Kobane in January - a key symbolic and strategic blow against the militant group which has emboldened the Kurdish movement in Turkey.
"Our representatives will be in the Parliament," said 41-year-old Selcuk Atasever. "They will advocate Kurdish rights."
- AFP