Muslim Brotherhood candidate Mohammed Morsi was declared the first democratically elected president of Egypt Sunday afternoon, ending months of speculation as to who would replace deposed leader Hosni Mubarak.

The carrying out of free elections in Egypt was a stunning development in a country that had been governed by autocracy until a popular revolution ousted Mubarak in January 2011.

Judge Farouk Sultan, chairman of Egypt's election committee, announces the result of the presidential election at the State Information Service headquarters in Cairo, Egypt. (photo credit: AP/Egypt State TV) MANDATORY CR

Judge Farouk Sultan, chairman of Egypt's election committee, announces the result of the presidential election at the State Information Service headquarters in Cairo, Egypt. (photo credit: AP/Egypt State TV)

Morsi, who won an earlier round of presidential voting, was declared the winner with 51.7 percent of the vote, compared to 48.3 percent for his opponent Ahmed Shafiq. A margin of about 800,000 votes.

Officials said 843,250 votes were declared void.

Some 51% of Egypt’s voting-eligible public cast ballots in the runoff election.

The announcement by election official Farouk Sultan came after a 45-minute delay and a long prologue.

Tahrir Square erupted into loud cheers and mass celebrations as the winner was announced, with thousands of people waving flags, shouting and dancing. Hundreds of thousands of people massed there to mark the victory.

“As Egyptians celebrate their freedom, we pay special tribute to the martyrs of the great Egyptian revolution, their blood didn’t go in vain,” the Brotherhood tweeted shortly after the announcement.

Morsi’s spokesman Ahmed Abdel-Attie said words cannot describe the “joy” in this “historic moment.”

“We got to this moment because of the blood of the martyrs of the revolution,” he said. “Egypt will start a new phase in its history.”

Shortly after the announcement, Morsi resigned from the Muslim Brotherhood, staying true to his pre-election pledge to serve as a president of “all Egyptians.”

The victory announcement was the culmination of a tumultuous, 16-month transition that was supposed to bring democratic rule, but was tightly controlled and curtailed by the military rulers who took power from Mubarak.

Field Marshall Hussein Tantawi, who headed the country’s caretaker military junta, congratulated Morsi after the win, according to Egyptian state television.

President Barack Obama telephoned the US-educated Morsi to congratulate him on his victory and offer continued support for Egypt’s transition to democracy. The White House said Morsi expressed appreciation for Obama’s call and “welcomed US support for Egypt’s transition.”

The Obama administration had expressed no public preference in the presidential race.

UN Secretary-General Ban Ki-moon also congratulated Egypt’s president-elect and commended the Egyptian people for the peaceful atmosphere of their first free presidential election in history.

UN spokesman Martin Nesirky said in a statement that Ban “trusts that the President-elect will spare no effort in ensuring the people of Egypt realize their aspirations for greater democracy.” The UN chief “stresses the need to strengthen and build strong, independent institutions and to allow civil society to flourish.”

The reaction from Israel was subdued, with Prime Minister Benjamin Netanyahu saying he respected the results of Egypt’s democratic process and hoped the peace agreement between the two countries would remain intact. Ecstatic residents in the Hamas-ruled Gaza Strip filled the streets, fired guns in the air and handed out candy.

Speaking on Egyptian television Sunday evening, Morsi declared he had a “message of peace. We will respect all international agreements.” He did not mention Israel but the remark seemed to be a reassuring nod to respecting the peace treaty.

It was a stunning victory for the Muslim Brotherhood, a party that was outlawed under Mubarak. But the liberal and secular youth groups that drove the uprising were left wondering whether Egypt has taken a step toward becoming a repressive Islamist state, or a new power sharing agreement between Morsi and the military — the traditional power brokers.

“This is not the best scenario I anticipated,” said Sarah Kamal, a liberal activist who was in Tahrir Square when Morsi’s victory was announced. She ululated and cheered for him despite criticism from many of her friends that Morsi would endanger a secular Egypt.

“I know they have sold the revolution short before. But they are better than the ‘felool,’” she said, referring to the remnants of the old regime. “I will stand with the Brotherhood against the military for now, and later I will fight off the Brotherhood’s hold,” she added.

Throughout the campaign, the Islamist Brotherhood attempted to paint Shafiq, a former prime minister, as the continuation of Mubarak’s regime, which ruled Egypt for 30 years.

Results were originally scheduled to be released on Thursday, but were pushed back as officials said they needed more time to tally the votes.

Both Shafiq and Morsi had previously announced that they had won.

Israel has expressed worries over the future of ties with Egypt should the hard-line Brotherhood take power.

The announcement was preceded by heavy police presence in Cairo and other places to counter possible protests, adding to tension in the country. The crossing between Gaza and Egypt was ordered closed just before the announcement, according to Egyptian daily Al-Ahram.

The announcement of the president was supposed to be the end of Egypt’s post-uprising transition to democracy. However, the military made a series of last minute-moves that stripped the office of president of most of its major powers and kept those powers concentrated in the hands of the military. A court ruling a few days before that dissolved the freely elected parliament that was dominated by the Muslim Brotherhood.

In Cairo’s Tahrir Square, the birthplace of the pro-democracy uprising, a swelling crowd of thousands gathered in sweltering midday heat awaiting the announcement. They were a mix of supporters of the Muslim Brotherhood and ultraconservative Islamists known as Salafis along with some of the revolutionary youth groups that drove last year’s uprising. A separate pro-Shafiq rally of some 2,000 protesters gathered in the northern Cairo district of Nasr City.