EGYPT HAS taken another halting step toward democracy — and avoided a plunge into chaos. By recognizing the victory of Muslim Brotherhood candidate Mohamed Morsi in a presidential runoff election, the ruling military has sidestepped the unbridled domestic conflict and international censure it might have provoked had it tried to void or manipulate the voting results. For the first time in its history, Egypt has a freely elected president.

Whether that development becomes a foundation to build on or a prelude to further destabilization in the Arab world’s most populous country will depend on whether the military and Islamists can find a modus vivendi based on democratic principles.

Unfortunately neither has so far shown a sufficient willingness to do so. Having initially announced that it would neither seek a majority in parliamentary elections nor nominate a candidate for president, the Brotherhood’s Freedom and Justice party did both. Using the leading position in parliament it won, it tried to dominate the selection of a constitution-writing committee, causing the process to break down.

The ruling military council and other remnants of the former autocratic regime under Hosni Mubarak have been far more destructive. After Mr. Mubarak’s ouster last year, the generals failed to restore public security or protect minorities such as Egypt’s Christians, mismanaged the economy, and repressed secular liberals, including nongovernmental groups funded by the United States. As the presidential runoff approached, the council issued an edict stripping power from the presidency, while judges appointed under the Mubarak regime ordered the dissolution of parliament. Martial law was restored.

Many Egyptians feared that the military would complete a creeping coup by awarding the presidency to former prime minister Ahmed Shafiq despite clear evidence that he lost the June 17 vote to Mr. Morsi. That this did not take place may reflect an accommodation in behind-the-scenes negotiations between the two sides. The Obama administration also probably contributed positively by urging the military to respect the election results and hinting that U.S. aid might be at risk.

The United States should now press the military to hand over full governing power to Mr. Morsi’s new administration by June 30, as it pledged to do, and to refrain from curtailing his tenure. A diverse and fully representative body is needed to immediately begin work on a constitution that will consolidate a democratic rule of law, including civilian control over the military. A fairly elected parliament must be seated. For that to happen, the Muslim Brotherhood probably will have to make some concessions to the military. More important, it will have to build a broad alliance of pro-democracy forces, reassure Christian and secular Egyptians, and set aside most of its ideological agenda.

Egypt still has a chance to complete a democratic transition, but it will require more mature political behavior by all sides. Strong encouragement by the United States — particularly to the recalcitrant generals of the old order — can help.