This week's first-round vote has polarised Egyptians between those determined to avoid handing the presidency back to a man from Mubarak's era and those fearing an Islamist monopoly of ruling institutions. The run-off will be held on June 16 and 17.
The election marks a crucial step in a messy and often bloody transition to democracy, overseen by a military council that has pledged to hand power to a new president by July 1.
The second round threatens further turbulence. Opponents of Mr Shafiq have vowed to take to the streets if he is elected. But to supporters, Mr Shafiq's military background offers reassurance he can restore security, a major demand of the population 15 months after Mubarak's departure.
A victory for the Brotherhood's Mohamed Mursi could worsen tensions between resurgent Islamists and the army, which sees itself as the guardian of the state.
Christians and secular liberals anxious about their own freedoms and the fate of Egypt's vital tourist industry will fret about a promised Brotherhood push for Islamic law. The revolutionaries who helped topple Mubarak now face what they see as a dispiriting choice between a conservative Islamist and a hardliner of the old guard.
"To choose between Shafiq or Mursi is like being asked do you want to commit suicide by being set on fire or jump in a shark tank," Adel Abdel Ghafar wrote on Twitter, a networking tool used to great effect against Mubarak. Tareq Farouq, 34, a Cairo driver, said: "I'm in shock. How could this happen? The people don't want Mursi or Shafiq. We're sick of both. They are driving people back to Tahrir Square."
Many Christians, who form about a tenth of Egypt's 82 million people, complained of discrimination in Mubarak's day, but are likely to vote for Mr Shafiq in preference to an Islamist.
If Mr Mursi becomes president, Islamists will control most ruling institutions – but not the military – consolidating electoral gains made by fellow-Islamists in other Arab countries in the past year.
Israel has nervously watched the Islamists in Egypt, its old enemy until a 1979 peace treaty. Mr Mursi vaguely advocates a "review" of the pact, whereas Mr Shafiq has vowed to uphold it.
The bluntly spoken military man came from behind in a race in which former Arab League chief Amr Moussa and ex-Brotherhood member Abdel Moneim Abol Fotouh were early favourites.
Mr Shafiq's late surge reflected the anxiety of many Egyptians about a breakdown of law and order and the often violent political disputes that have punctuated an army-led transition since a revolt ousted Mubarak in February last year.
The Brotherhood said yesterday the run-off would be between Mr Shafiq and Mr Mursi after almost all votes were counted. A member of Mr Shafiq's campaign also said Mr Mursi and Mr Shafiq were in the lead, but that counting was not complete. Official results are not expected until Tuesday.
Aides to other candidates put Mr Mursi ahead but gave shifting tallies for second place through the night. Officials said about half of Egypt's 50 million eligible voters had cast ballots.