Egypt's Mohamed Morsi, the first Islamist to be elected president of the Arab world's most populous nation, said Sunday he will be a leader "for all Egyptians" and called for national unity after a polarising race.
Morsi, the country's first elected leader since a popular uprising ousted president Hosni Mubarak, won 51.73 percent of the vote against ex-premier Ahmed Shafiq.
"I will be a president for all Egyptians," Morsi said just hours after he was declared the winner.
"I call on you, great people of Egypt ... to strengthen our national unity," he said, adding that national unity "is the only way out of these difficult times".
Morsi, who resigned from the Muslim Brotherhood to take the top job, thanked the "martyrs" of the uprising for the victory and stressed that "the revolution continues".
The 60-year-old engineer also vowed to honour international treaties.
"We will preserve all international treaties and charters ... we come in peace," Morsi said.
Egypt was the first Arab country to sign a peace treaty with Israel, in 1979.
The election comes after 18 months of a tumultuous military-led transition from Mubarak's rule, marked by political upheaval and bloodshed.
"The winner of the election for Egyptian president on June 16-17 is Mohamed Morsi Eissa al-Ayat," said the head of the electoral commission, Faruq Sultan earlier.
The announcement saw hundreds of thousands of Morsi supporters erupt in celebration in Cairo's Tahrir Square, waving flags and posters of the Islamist leader, who was jailed during the uprising that overthrew Mubarak early last year.
"God is greatest" and "down with military rule" they chanted, as fireworks went off over the square.
Across Cairo, cars sounded their horns and chants of "Morsi, Morsi" were heard.
Losing candidate Shafiq, who was widely perceived as the military's candidate, joined the country's military ruler Field Marshal Hussein Tantawi in congratulating Morsi.
"General Ahmed Shafiq sent a message to Mohamed Morsi congratulating him on his victory," the official MENA news agency reported. Earlier state television said Tantawi had congratulated Morsi.
Morsi won with 13,230,131 votes against Shafiq's 12,347,380, Sultan said. The election, in which more than 50 million voters were eligible to cast their ballot, saw a 51.8 percent turnout.
Morsi resigned from his posts in the Muslim Brotherhood and its Freedom and Justice Party, which he headed, after he was declared the winner, the Brotherhood announced.
And the interim head of the Coptic Church, many of whose faithful have feared the rise of Islamists, also congratulated Morsi.
Shafiq supporters who had gathered to hear the result with his campaign team in the suburbs of Cairo were devastated by the result.
Some women screamed and others cried as several men held their heads between their hands in despair.
"It's a very sad day for Egypt. I don't think Morsi is the winner. I'm very sad that Egypt will be represented by this man and this group," Shafiq supporter Maged told AFP.
The capital was tense before the announcement, with the city's notoriously busy streets deserted and shops and schools closed.
Extra troops and police were deployed as military helicopters flew overhead.
The road to parliament was closed to traffic, and security was tightened around vital establishments as Egyptians waited nervously for the result.
The election has polarised the nation, dividing those who feared a return to the old regime under Shafiq from others who wanted to keep religion out of politics and who fear the Brotherhood would stifle personal freedoms.
Shafiq ran on a strong law-and-order platform, pledging to restore security and stability. He is himself a retired general, but as a Mubarak-era minister he is reviled by the activists who spearheaded the 2011 revolt.
President-elect Morsi was the Islamists' fallback representative after their deputy leader Khairat El-Shater was disqualified.
In campaigning he sought to allay the fears of secular groups and the sizeable Coptic Christian minority by promising a diverse and inclusive political system.
Both Morsi and Shafiq had claimed victory in the election for a successor to Mubarak, and tensions heightened after the electoral commission delayed announcing the official outcome.
The delay in announcing the result of the June 16-17 run-off, initially scheduled for Thursday, had raised suspicions that the outcome of the election was being negotiated rather than counted.
On Friday, the SCAF warned it would deal "with utmost firmness and strength" with any attempts to harm public interests.
The Brotherhood warned against tampering with the election results, but also said it had no intention of instigating violence.
It has rejected a constitutional declaration by the military that strips away any gains made by the Islamist group since the popular uprising which forced Mubarak to stand down in February last year.
The document dissolves the Islamist-led parliament and gives the army a broad say in government policy and control over the new constitution. It was adopted just days after a justice ministry decree granted the army powers of arrest.