The Muslim Brotherhood's Mohammed Mursi has been declared the winner of Egypt's presidential election run-off.
He won 51.73% of the vote, beating former PM Ahmed Shafiq, the Higher Presidential Election Commission said.
Speaking later in a TV address, Mr Mursi hailed a "historic day" for the nation, and said he would be a president for all Egyptians.
There have been scenes of jubilation in Cairo's Tahrir Square, where Mr Mursi's supporters gathered.
They have been holding a vigil there for days in protest at a series of decrees by the ruling Supreme Council of the Armed Forces (Scaf) which they say are designed to reduce or constrain the power of the president, and entrench the power of the military.
As night fell, tens of thousands continued to throng Tahrir Square. Mr Mursi was due to address the nation in his first speech after being declared president.
"Down with military rule" his supporters chanted amid wild cheering and explosions of fireworks after the result was announced
Across Cairo, cars sounded their horns amid crowds chanting "Mursi, Mursi".
Supporters of Mr Shafiq, who had been holding a rally in the capital's northern suburb of Nasser City, were stunned by the result.
There was screaming and crying and people were seen holding their heads in despair.
'Unity and love'
Mr Mursi thanked God and "the blood, tears and sacrifices" of his fellow countrymen.
"Today I am a president for all Egyptians, wherever they may be," he said.
"Thanks to our unity and our love for each other, we will able to make a respectable future for ourselves."
The president-elect saluted the "martyrs of the revolution", which last year swept away the authoritarian rule of Hosni Mubarak.
But he also praised the army and police, who many Egyptians see as playing negative roles over the past year.
The BBC's Kevin Connolly in Cairo says Mr Mursi's victory is a moment of profound change for Egypt.
The Muslim Brotherhood, that has seen many of its members put in prison, now has one of its leaders being sent instead to the presidential palace, he says.
Security had been tight for the announcement, with tanks and troops deployed around the election commission's headquarters.
Egypt's military ruler, Field Marshal Hussein Tantawi, congratulated Mr Mursi on his victory, state-run Nile News TV reported.
UK Foreign Secretary William Hague also congratulated Mr Mursi, saying it marked "an historic moment for Egypt".
A White House statement said: "We believe it is essential for the Egyptian government to continue to fulfil Egypt's role as a pillar of regional peace, security and stability.
Israeli Prime Minister Benjamin Netanyahu said he hoped the long-standing peace treaty between the two countries would continue.
Hours after the result, Mr Mursi resigned from his positions within the Muslim Brotherhood including his role as chairman of its Freedom and Justice Party (FJP) as he had pledged to do in the event of his victory.
Mr Mursi has promised that his leadership will be inclusive, and has courted secular and Christian voters.
Earlier, the head of the Higher Presidential Election Commission, Farouq Sultan, began the news conference by saying the declaration of the result had been "marred by tension and a bad atmosphere".
He spent several minutes announcing minor amendments to the vote tallies before suddenly revealing that Mr Mursi had won 13,230,131 votes (51.73%), compared with Mr Shafiq's total of 12,347,380, or 48.27%.
The turnout in last weekend voting was 51.58%, he added.
The military council has ruled Egypt since last year's revolution that toppled former president Hosni Mubarak, and has promised to oversee a transition to democracy.
However, a series of decrees has led many to believe it intends to cling on to power.
On 13 June, the justice ministry gave soldiers the right to arrest civilians for trial in military courts until the ratification of a new constitution.
The Scaf then issued a decree on Friday dissolving parliament in line with a Supreme Constitutional Court ruling that the law on elections to the lower house was invalid because party members had been allowed to contest seats reserved for independents.
Two days later, just as the polls were closing in the run-off, the generals issued an interim constitutional declaration that granted them legislative powers and reinforced their role in the drafting of a permanent constitution. The military was also exempted from civilian oversight.
Then on Monday, Field Marshal Tantawi announced the re-establishment of a National Defence Council, putting the generals in charge of Egypt's national security policy.
The generals have vowed to hand over power to the new president by 30 June, but their decision to dissolve parliament means Mr Mursi could take office without the oversight of a sitting legislature and without a permanent constitution to define his powers or duties.