Egypt's Mohamed Morsi became the first Islamist to be elected president of the Arab world's most populous nation and after a divisive election battle pledged to be a leader for all Egyptians.
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 added. National unity "is the only way out of these difficult times".
Morsi, who resigned from the Muslim Brotherhood to take the top job, also thanked the "martyrs" of the uprising for the victory.
"The revolution continues," he added.
The 60-year-old engineer also vowed to honour international treaties, adding: "We come in peace." 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 16-17 June is Mohamed Morsi Eissa al-Ayat," the head of the electoral commission, Faruq Sultan announced earlier on Sunday.
The announcement saw hundreds of thousands of Morsi supporters erupt in celebration in Cairo's Tahrir Square, the symbolic heart of the uprising that brought down the Mubarak regime in February 2011.
Cheering Egyptians waved flags and posters of the Islamist leader, who was jailed during last year's uprising.
"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.
US President Barack Obama congratulated Morsi on his victory and pledged his country's support for Egypt's transition to democracy, a White House spokesman said.
Obama had also called Shafiq to encourage him to keep supporting the democratic process "and working to unify the Egyptian people."
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, the official MENA news agency reported.
The interim head of the Coptic Church, many of whose faithful have feared the rise of Islamists, also congratulated Morsi.
Morsi won with 13 230 131 votes to Shafiq's 12 347 380, said Sultan, announcing the official results. The election, in which more than 50 million voters were eligible to cast their ballot, saw a 51.8 percent turnout.
Following Morsi's victory, the Muslim Brotherhood announced that he had resigned his posts in the group and in its Freedom and Justice Party, which he headed.
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," one Shafiq supporter, Maged, told AFP.
"I don't think Morsi is the winner. I'm very sad that Egypt will be represented by this man and this group."
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 and 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 had polarised the nation.
While some feared a return to the old regime if Shafiq won, others wanted to keep religion out of politics, fearing that the Brotherhood would stifle personal freedom.
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 from running.
During his campaign 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 rose after the electoral commission delayed announcing the official outcome of 16-17 June second-round run-off.
The results had initially been due on Thursday and the delay raised suspicions that the outcome of the election was being negotiated rather than counted.
But the dispute between the Brotherhood and the military remains unresolved.
The Brotherhood had rejected a constitutional declaration by the military that dissolves the Islamist-led parliament and gives the army a broad say in government policy and control over the new constitution.
The move effectively strips away any gains made by the Islamist group since the popular uprising.
It was adopted just days after a justice ministry decree granted the army powers of arrest.