Zimbabwean lawmakers celebrate at the parliament in Harare after Robert Mugabe’s resignation. (Jekesai Njikizana/AFP/Getty Images)

Robert Mugabe, Zimbabwe's leader for nearly four decades, resigned Tuesday after being sidelined by the military and cast out by his own political party, marking the end of a tumultuous reign that lasted from the country's independence through its economic collapse.

The capital erupted in celebration, with crowds pouring into the streets, dancing atop military vehicles and singing late into the night. Many people cried tears of joy as news of Mugabe's resignation spread.

This nation of 16 million people now faces a deeply uncertain period, with a fragmented opposition, no clear path to elections and a controversial heir to power, but the nation was at least momentarily united by the removal of its despotic leader.

Mugabe's exit marks a historic moment that will echo across Africa, where he was among the last surviving heroes of the anti-colonial struggle to remain in power, a man who was initially lionized as a liberator but was increasingly seen as autocratic and brutal. He presided over the stunning collapse of a nation that was known as the breadbasket of the region at its 1980 independence.

His resignation could send a message to other strongmen on the continent who have clung to power for years, defying or manipulating their constitutions. Still, Mugabe's likely successor, former vice president Emmerson Mnangagwa, who was fired by the president this month, worked closely with him for years and is not viewed as a reformer.  

"The dictator is gone!" Takudzwa Jonasi, 32, a chemical engineer, shouted as he celebrated with a jubilant crowd outside parliament. 

"For our generation, we have never seen any change. We were not allowed to exercise our rights," he added. Like many young Zimbabweans, Jonasi has known no other leader. 

"I have no words. We are finally free!" exclaimed Shoes Tazviwan, 36, a chef who had also joined the demonstrations.

In the end, the world's oldest head of state was a victim of his own allies. After years of purging members of his inner circle, Mugabe had alienated the leaders of Zimbabwe's military, who detained him and seized control of the country's government Nov. 14.

After days of negotiations — and the largest anti-government demonstration in the country's history — the 93-year-old leader went quietly, sending his resignation letter to parliament, where it was read by the speaker, Jacob Mudenda.

The surprise announcement came as parliament was debating Mugabe's impeachment. Shortly before 5 p.m. local time, the speaker halted the discussion and announced the president's departure. The body burst into cheers. Mudenda announced that a new president would be named Wednesday.

According to the speaker, Mugabe's letter said he was resigning for "the welfare of the people of Zimbabwe and the need for a peaceful transfer of power."

Mugabe's resignation leaves Zimbabwe at a crossroads — with the military technically in charge of the country and a wide array of political groups now angling for power.

Mnangagwa, the president's likely successor, is a longtime Mugabe ally, nicknamed "the Crocodile" for his reputation for shrewd but often brutal tactics. The State Department said in 2000 that he was "widely feared and despised throughout the country" and "could be an even more repressive leader" than Mugabe.

For the moment, Mnangagwa appears to have the backing of Mugabe's former party and the military, but Zimbabwe's opposition remains fragmented, and politicians and activists will now try to seize upon the president's resignation to carve out their own positions in whatever government comes next.

For the past week, Zimbabweans have been united by their opposition to their long-ruling, autocratic leader, and many here expressed hope that the rare period of unity would lead to the formation of a broad coalition.

"Let's agree for this moment that the enemy of my enemy is my friend," said Fadzayi Mahere, a lawyer and politician.

As soon as Mugabe's resignation was announced on the radio, car horns started blaring and drivers pumped their fists in the air. In front of the parliament, people danced on car roofs and blasted music, waving Zimbabwean flags as the sun set over Harare.

One man in a park fell to his knees in celebration with his arms outstretched. Another kissed the ground.

"It's a new day for us. I've been carrying my exam results in my purse looking for a job. There is nothing. He has ruined our economy," said Sibongile Tambudzi, 24, who pulled out the results and then vanished into a dancing crowd.

"I don't know what will happen tomorrow," said Precious Mazayi, the owner of a security company. "I don't even know who the president is right now. But for now, let us just celebrate. We have waited so long for this." 

The celebrations only grew as the night went on. But for some older Zimbabweans who remembered the euphoria in 1980, when Mugabe ascended to power after independence from Britain and the end of white-minority rule, the excitement was tempered with fear of what might follow.

"Right now, to be honest, I'm apprehensive. We were happy in 1980, and we saw what happened next," said Vincent Tanyanyiwa, 45, a professor of environmental policy at a Harare university.

"We need to be careful. We have a new chance here. Let's not spoil it."

Civilians join soldiers in celebration after the resignation of Zimbabwe’s president in Harare. (Marco Longari/AFP/Getty Images)

The U.S. Embassy in Harare said in a statement that Mugabe's departure marked a "historical moment" for the country, but it urged the new leadership to allow elections in which people can "voice their opinions without fear."

Mugabe's life traced the changes that swept through southern Africa in the 20th and 21st centuries. He was the son of a carpenter in the British colony of Southern Rhodesia and rose to lead the fight against the white-minority government of Rhodesia, as the country was known then.

When he came to power in 1980, Mugabe was a self-identified Marxist-Leninist whose intellect and political flair brought him support from across the world. In 1983, then-Vice President George H.W. Bush called him a "genuine statesman." In 1994, he was knighted by Queen Elizabeth II. 

But his government's descent was swift and dramatic. In the early 1980s, he was accused of backing the murder of 20,000 people of the Ndebele tribe, whom he considered dissidents. In the 1990s, economic mismanagement brought hyperinflation to Zimbabwe, resulting in the printing of bank notes of 100 trillion Zimbabwean dollars. 

In the early 2000s, in an effort to satisfy his political allies and reaffirm his anti-colonial bona fides, Mugabe presided over the violent seizure of farmland belonging to white Zimbabweans. Much of that land sat fallow after it was redistributed. A country once known for its agricultural production was forced to start importing food. 

As Mugabe grew older and more frail — and as opposition to his presidency mounted — Zimbabweans began talking openly about how the reign of the "old man" might end. For years, rumors circulated that he was critically ill, but Mugabe always reemerged, giving cogent, if meandering, speeches into his 90s. 

"Zimbabwe will never again be a colony," became his trademark rallying cry, which meant little to young Zimbabweans who found it increasingly difficult to find work. The unemployment rate soared over 50 percent. More than 2 million Zimbabweans moved to South Africa in search of jobs.

Grace Mugabe, wife of then-President Robert Mugabe, delivers a speech during a rally of Zimbabwe’s ruling ZANU-PF party in Bulawayo on Nov. 4. (Zinyange Auntony/AFP/Getty Images)

Few analysts predicted that his presidency would end with a coup. But after Mugabe fired his vice president, Mnangagwa, paving the way for his controversial wife, Grace Mugabe, to succeed him, Zimbabwe's security forces revolted.

Since the army's action a week ago, there has been no sign of Grace Mugabe, nicknamed "Gucci Grace" for her expensive shopping sprees. In 2015, she purchased a $1.3 million diamond ring and then demanded a refund when she found it unsatisfactory. The average per capita gross domestic product in Zimbabwe is $1,008. 

It also remained unclear what would happen to Robert Mugabe — whether he would remain in Zimbabwe or spend the rest of his life in exile. Earlier this year, his wife said he could run for reelection "as a corpse" if he died before the vote.

But Tuesday, amid the celebrations outside his office, Mugabe's portraits were taken down from shops and offices across the city, and road signs bearing his name were destroyed or vandalized.

"It's a new era. Whatever comes next, it's a new era," said Tanyanyiwa, the professor. 

Brian Murphy and William Branigin in Washington contributed to this report.