Mohammed Morsi of the Muslim Brotherhood party has been named as the first democratically elected president of Egypt.
Huge cheers erupted and firecrackers were let off in Tahrir square as the head of the election commission made the announcement.
Thousands of Morsi supporters had camped in the square, the site in which the Arab Spring uprising began last year.
There had been fears that if Shafiq had won, it could have provoked violent clashes between Muslim brotherhood supporters and those of former Prime Minister Shafiq. It is yet to be seen how Shafiq supporters will respond to the results.
The Muslim Brotherhood's Mohammed Mursi gained 51.73% of the vote, while Ahmed Shafiq got 48.27%, the election commission says.
Scenes broadcast from Cairo so far showed intense jubilation that the country had a new president, and one not associated with the old regime.
Both candidates projected victory earlier this month, and security is still on high alert as tanks stand guard outside the Cairo election commission headquarters.
As well as tension between both parties, there is apprehension over whether Egypt's ruling military council will be willing to hand over power to the incoming president, despite the election results. The ruling military council is due to hand over power to Morsi by 30 June.
Speaking to Egypt's Nile TV, ruling general Field Marshal Mohammed Hussein Tantawi, swept aside the historically tense relationship between the Muslim brotherhood and the military to congratulate Morsi.
A "million man march" was arranged by the Muslim brotherhood after the ruling military council granted itself all-pervasive new powers, a move which the Islamist party described as a "military coup."
The new decrees issued by Supreme Council of Armed Forces (Scaf) allow the military total control over the country's legislation, army and budget. Scaf also insist that new parliamentary elections cannot be held until a constitution is agreed.
Anxiety over the possibility that the military was reluctant to hand power over to the elected president was already brewing after the Supreme Court dissolved parliament last week.
Foreign Secretary William Hague said in a statement on Tuesday that he was concerned by recent announcements of "the dissolution of parliament and the reintroduction of powers of arrest and detention for the military".
"The process of drafting a new, inclusive constitution and the holding of new parliamentary elections should be taken forward as soon as possible" he added.
Similarly Washington aired its "deep concern" over the developments and called on the army to hand over "full power" to civilians.
A satirical headline in independent newspaper al-Masry al-Youm read "Military Transfers Power - to Military," reports Reuters, scorning any official announcement by the Scaf.
The reality is that whoever wins, the new leader will be forced to work under the military council who have imposed restrictions on presidential power, leading some commentators to declare that the election has been hijacked by a military coup. On Twitter, former UN diplomat Mohamed ElBaradei described the move as a "setback for democracy".