CAIRO — Egypt exploded with joy, tears, and relief after President Hosni Mubarak resigned as president, forced out by 18 days of mass protests that culminated in huge marches Friday on his presidential palaces and state television. The military took power after protesters called for it to intervene and oust their leader of three decades.

“The people ousted the regime,” rang out chants from crowds of hundreds of thousands massed in Cairo’s central Tahrir Square and outside Mubarak’s main palace several miles away in a northern district of the capital.

The crowds in Cairo, the Mediterranean city of Alexandria and other cities around the country danced, chanted “goodbye, goodbye,” and raised their hands in prayer in an ecstatic pandemonium as fireworks and car horns sounded after Vice President Omar Suleiman made the announcement on national TV just after nightfall.

“Finally we are free,” said Safwan Abou Stat, a 60-year-old in the crowd of protesters at the palacer. “From now on anyone who is going to rule will know that these people are great.”

President Barack Obama says Hosni Mubarak’s resignation is the beginning, not the end, of the transition to democracy in Egypt.

Speaking just hours after Mubarak relinquished the presidency, Obama said the longtime Egyptian leader responded to his people’s hunger for change. But he warned that there will still be difficult days ahead for Egypt as it works toward free and fair elections.

Mubarak had sought to cling to power, handing some of his authorities to Suleiman while keeping his title. But an explosion of protests Friday rejecting the move appeared to have pushed the military into forcing him out completely. Hundreds of thousands marched throughout the day in cities across the country as soliders stood by, besieging his palace in Cairo and Alexandria and the state TV building. A governor of a southern province was forced to flee to safety in the face of protests there.

His fall came 32 years to the day after the collapse of the shah’s government in Iran.

The protests have already echoed around the Middle East, with several of the region’s autocratic rulers making pre-emptive gestures of democratic reform to avert their own protest movements. The lesson many took: If it could happen in three weeks in Egypt, where Mubarak’s lock on power had appeared unshakeable, it could happen anywhere.

The United States at times seemed overwhelmed trying to keep up with the pace, fumbling to juggle its advocacy of democracy and the right to protest, loyalty to longtime ally Mubarak and fears of Muslim fundamentalists gaining a foothold. Neighoring Israel watched the development with growing unease, worried that their 1979 peace treaty could be in danger. It quickly demanded on Friday that post-Mubarak Egypt continue to adhere to it.

Friday was the biggest day of protests yet in the upheaval that began Jan. 25. The movement grew for the Internet organizing of small groups of youth activists into a mass movement that tapped into widespread discontent with Mubarak’s authoritarian lock on power, corruption, economic woes and widespread disparities between rich and poor.

The question now turned to how the military, long Egypt’s most powerful institution and now its official ruler, will handle the transition in power. Earlier in the day, the Armed Forces Supreme Council — the military’s top body — vowed to guide the country to greater democracy. State TV said a new statement by the military would be issued Friday evening.

Vice President Suleiman — who appears to have lost his post as well in the military takeover — appeared grim as he delivered the short announcement.

“In these grave circumstances that the country is passing through, President Hosni Mubarak has decided to leave his position as president of the republic,” he said. “He has mandated the Armed Forces Supreme Council to run the state. God is our protector and succor.”

Nobel Peace laureate Mohammed ElBaradei, whose young suporters were among the organizers of the protest movement, told The Associated Press, “This is the greatest day of my life.”

“The country has been liberated after decades of repression,” he said adding that he expects a “beautiful” transition of power.

Outside Mubarak’s Oruba Palace in northern Cairo, women on balconies ululated with the joyous tongue-trilling used to mark weddings and births.

“Finally we are free,” said Safwan Abo Stat, a 60-year-old in the crowd of protesters at the palace. “From now on anyone who is going to rule will know that these people are great.”

Another, Mohammed el-Masry, weeping with joy, said he had spent the past two weeks in Tahrir before marching to the palace Friday. He was now headed back to the square to join his ecstatic colleagues. “We made it,” he gasped.