CAIRO (AP) — Mohammed Morsi was declared Egypt's first Islamist president on Sunday after the freest elections in the country's history, narrowly defeating Hosni Mubarak's last Prime Minister Ahmed Shafiq in a race that raised political tensions in Egypt to a fever pitch.

The country's last four presidents over the past six decades have all came from the ranks of the military. This is the first time modern Egypt will be headed by an Islamist and by a freely elected civilian.

Throngs of Morsi supporters in Cairo's Tahrir Square erupted in cheers and dancing when the result was read out on live television. Some released doves with his pictures over the square where the uprising that ousted Mubarak last year was born. Others set off fireworks.

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."

The 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.

The outcome will not put an end to the main power struggle in the country now between Morsi's Muslim Brotherhood and the military.

The election commission said Morsi won with 51.7 percent of the vote versus 48.3 for Shafiq. Turnout was 51 percent.

Farouk Sultan, the head of the commission, described the elections as "an important phase in the end of building our nascent democratic experience."