Mr. Morsi, 60, an American-trained engineer and former lawmaker, is the first Islamist elected as head of an Arab state. But 16 months after the military took over at the ouster of President Hosni Mubarak, Mr. Morsi’s victory is an ambiguous milestone in Egypt’s promised transition to democracy.

After a week of doubt, delays and fears of a coup since a public ballot count showed Mr. Morsi ahead, the generals have proven a measure of respect for at least some core element electoral democracy — they have accepted a political opponent over their ally, former General Ahmed Shafik, after a vote that international monitors called credible.

But Mr. Morsi’s recognition as president does little to resolve the larger standoff between the generals and the Brotherhood over the institutions of government and the future constitution. With just two weeks to go until their promised exit from power by June 30, the generals instead shut down the democratically elected and Islamist-led Parliament; took over its powers to make laws and set budgets; decreed an interim constitution stripping the new president of most of his power; and re-imposed martial law by authorizing soldiers to arrest civilians. And the general gave themselves an effective veto over provisions of a planned permanent constitution as well.

As recently as this morning the capital was tense with fears that the panel of Mubarak-appointed judges overseeing the vote would declare Mr. Shafik president, completing a full military coup. Banks, schools and government offices closed early for fear of violence in the streets.

Tens of thousands of Brotherhood supporters and allies against military rule had gathered in Tahrir Square for the sixth day of a sit-in demanding the military roll back its power grab. The throngs hushed as transistor radios in the square began broadcasting the election commissioner’s rambling speech to introduce the official results.

Then came the moment. The square erupted as the numbers came through: Mr. Morsi had won 51.7 percent of the runoff vote.

“Morsi, Morsi!,” the crowd chanted. “Down, down with military rule!” Small fireworks went off over the crowd, beaming Brotherhood supporters streamed in, swelling the crowd to perhaps a hundred thousand by nightfall. In a carnival atmosphere, vendors hocked cotton candy or threw pieces or fruit into the laughing crowd.

After 84 years as an often outlawed secret society struggling in the prisons and shadows of monarchs and dictators, the Brotherhood is now closer than ever to its stated goal of building an Islamist democracy in Egypt. “In my dreams I wanted this to happen, but it is unbelievable,” said Hudaida Hassan, a 20-year-old from Menoufiya.

Even in a victorious moment, however, the Brotherhood’s leadership acknowledged that the struggle was far from over: leaders immediately pledged to continue the sit-in, fighting on in the courts and in the streets to restore the Parliament. And in his first statement as president-elect, Mr. Morsi vowed to swear the oath of office before the seated Parliament and not before the Supreme Constitutional Court as the generals had decreed.

Field Marshall Mohamed Hussein Tantawi, the chairman of the military council, congratulated Mr. Morsi. The Brotherhood’s political arm said on its Web site that the official presidential guard who previously protected Mr. Mubarak had arrived at Mr. Morsi’s home to begin protecting him. It was a stark contrast from the days less than two years ago when the arrival of armed officers at the home of a Brotherhood leader inevitably meant a trip to one of Mr. Mubarak’s jails.

Fulfilling a campaign pledge to represent all Egyptians, Mr. Morsi resigned from the Brotherhood and its political arm, the Freedom and Justice Party. State media reported Sunday morning that the prime minister and cabinet would resign immediately, making way for Mr. Morsi to appoint his own team. As the Brotherhood has reached out to rebuild alliances with liberal and other secular activists for its contest with the generals, Mr. Morsi has pledged to name a prime minister and other top officials from outside the Brotherhood as part of a unity government.

At the same time, however, Mr. Morsi has always campaigned not as an individual with a vision of his own but rather as an executor of the Brotherhood’s platform. He was the group’s second choice nominee, put forward after the disqualification of the group’s lead strategist and most influential leader, Khairat el-Shater, and Mr. Morsi has vowed to carry out the program Mr. Shater has spent more than a year devising to reform and remake Egypt’s government ministries. Mr. Morsi and Mr. Shater have never effectively dispel accusations that Mr. Shater would wield the true power in a Morsi government.

Even after the two-month presidential campaign, Mr. Morsi remains an unfamiliar figure to most Egyptians. He earned a doctoral degree in materials engineering at the University of Southern California in t1982 — putting him in the United States for the tumultuous years after Islamists assassinated President Anwar Sadat and President Muabrak cracked down on the Brotherhood.

Those who knew him in Los Angeles say Mr. Morsi never appeared notably political or religious. But he returned to teach at Zagazig University in the Nile Delta, where he became a leader in the Brotherhood and eventually won of its first members of the Mubarak-dominated Parliament.

He was picked by higher-ups to lead the Brotherhood’s small parliamentary bloc, which then including just 18 members out of more than 500 lawmakers. He thus played a key role in the group’s first experiment in multiparty electoral democracy and coalition building. But in subsequent years, as he was elevated to the Brotherhood’s governing board, he gained the reputation as an internal enforcer, known for discouraging voices of dissent.

When the Brotherhood adopted a hypothetical draft party platform in 2007 that argued neither a women nor a non-Muslim should be eligible to be Egypt’s president, Mr. Morsi was a chief defender of those planks, internally and externally.

Nonetheless, the jubilation at Mr. Morsi’s victory appeared to energize his supporters, and in the moment even appeared to have won over some more secular Egyptians who had stayed on the sidelines of the Brotherhood’s tug of war with the military. Alaa Aswany, a novelist and critic who campaigned against Mr. Morsi in the runoff and has been one of the sharpest critics of the Brotherhood congratulated him Sunday.

“Congratulations for the Egyptian people,” Mr. Aswany wrote in a commentary online. “The will of the people was able to topple the old regime once more. Long live the revolution.”

Although it was clear as early as Monday morning that Mr. Morsi had won more votes than Mr. Shafik, the weeklong delay in the official results stirred widespread fears that the military-led government might seek to name Mr. Shafik as a decisive blow after its dramatic steps to hedge its own power.

Before the results were announced, the capital was as tense Sunday as on any day since the two and a half week revolt that brought down Mr. Mubarak. Army tanks and soldiers were deployed around the election commission, the Parliament and other institutions to prepare for possible violence. Foreign embassies warned their citizens to stay away from downtown. Banks, government offices and schools all closed early to allow students and employees to get off the streets.

His designation as president-elect will hand the Brotherhood and its allies an important anchor in its struggle for power with the military. The Brotherhood has sought to rebuild the partnership with more secular and liberal advocates of democracy that came together in the uprising against Mr. Mubarak, and Brotherhood leaders have vowed not to hold any negotiations with the generals without the participation of the other groups in their so-called “national front.”

In a statement, the White House referred to that promise, congratulating Mr. Morsi even as it called on him to reach out to Egypt’s non-Islamist constituencies: “We believe in the importance of the new Egyptian government upholding universal values, and respecting the rights of all Egyptian citizens — including women and religious minorities such as Coptic Christians.” The statement also called on the military to allow a full transition to a democratic government.

Official reaction in Israel was muted, congratulating the country on its election. Israeli officials have watched the roiling events next door in Egypt with trepidation over the past year and a half, reflecting concern that the Egyptian generals’ long honoring of a peace treaty with Israel would be up for reassessment under a new government.

In Gaza, however, where the Brotherhood-allied Hamas faction is predominant, wild celebrations broke out. Many gunmen took to firing long volleys in the air, leading to the death of a 24-year-old man and the wounding of two girls in Rafah, near the border crossing to Egypt.On its own, the Brotherhood’s control of the presidency will do nothing to reduce the calm the fierce polarization of Egyptian society. On Saturday night, a counter protest that reportedly grew to over 10,000 gathered in a neighborhood with a heavy concentration of military personnel to demonstrate in support of the ruling generals, Mr. Shafik and secular government. Mr. Shafik, Mr. Mubarak’s last prime minister, has campaigned with the support of the old ruling party elite as a new strongman who can bring back order after the 16 months of chaos.

Earlier in the day, a group of secular political leaders and lawmakers who call themselves liberals had held a televised news conference to declare their support for the generals and the dissolution of the Brotherhood-led Parliament. The praised the shutdown of Parliament as a victory for law and order, citing an unusually rushed court decision announced the day before. (The Brotherhood has respected the ruling but challenged its implementation.)

The secular politicians also accused the Brotherhood of “hijacking” the revolution and called it a threat to the “civil” character of the state. They dismissed the Brotherhood’s pledges to govern in coalition, respecting individual and minority rights, and instead accused the group of plotting to impose religious rule.

Incongruously, given Washington’s history of antagonism with the Islamists of the Muslim Brotherhood, the secular block argued that the United States was improperly attempting to sway the presidential race in favor of the Brotherhood, although American officials and the embassy have said they support only the democratic process regardless of the result.

Mr. Shafik was silent Sunday. But a handful of his supporters held their own angry protests to denounce the decision of the election commission.

**Mayy El Sheikh, Liam Stack and Kareem Fahim contributed reporting from Cairo, and Fares Akram from Gaza.