A letter from Mr Mugabe read out by the speaker of parliament said the decision was voluntary and he had made it to allow a smooth transfer of power.
The news abruptly halted an impeachment hearing that had begun against him.
The ruling Zanu-PF party says former vice-president Emmerson Mnangagwa will succeed Mr Mugabe, in power since 1980.
Mr Mnangagwa’s sacking earlier this month triggered a political crisis.
had been seen by many as an attempt to clear the way for Grace Mugabe to succeed her husband as leader and riled the military leadership, who stepped in and put Mr Mugabe under house arrest.
After the resignation announcement, lawmakers roared in jubilation.
Robert Mugabe: Hero or villain?
Resignation and reaction – as it happened
Mr Mugabe, 93, was until his resignation the world’s oldest leader. He had previously refused to quit despite last week’s military takeover and days of protests.
Media captionScenes of jubilation on the streets of the capital
According to the constitution his successor should be the current vice-president, Phelekezela Mphoko, a supporter of Grace Mugabe.
But Zanu-PF chief whip Lovemore Matuke told Reuters news agency that Mr Mnangagwa would be in office “within 48 hours”.
Emmerson Mnangagwa: The ‘crocodile’ who snapped back
Speaking from an undisclosed location earlier on Tuesday, Mr Mnangagwa said he had fled abroad two weeks ago when he learned of a plot to kill him.
Most people assumed that the only way Robert Mugabe would give up being president was to die in his bed. He probably thought so too.
In fact the last of the old-style 1970s and 80s liberation leaders, most untypically, resigned in writing. Perhaps that says something about the way the world has changed in the 21st Century.
No storming the presidential palace, no ugly end at the hands of a crowd like Col Gaddafi, no execution by firing squad like President Ceausescu of Romania, no hanging like Saddam Hussein.
Zimbabwe, in spite of everything Robert Mugabe visited upon it, is essentially a peaceable, gentle country. And despite all the immense crimes for which he was responsible, he is in some ways an intellectual, rather than a brutal thug along the lines of, say, Idi Amin.
He’ll be remembered for the massacres in Matabeleland in the 1980s, for the farm invasions of the 1990s and later, and for the brutal repression of the opposition Movement for Democratic Change when it seemed on course to win the 2008 presidential election.
The man who seems about to take his place, Emmerson Mnangagwa, was deeply involved in most of those crimes, yet people in Zimbabwe, like the outside world, will be so relieved to see Mr Mugabe go that they will be tempted to forget all that.
They’ll also forget the few unquestionably good things Robert Mugabe did. Zimbabwe, for instance, has an extraordinarily high literacy rate, because of him. But that’s certainly not what he’ll be remembered for.
UK Prime Minister Theresa May said Mr Mugabe’s resignation “provides Zimbabwe with an opportunity to forge a new path free of the oppression that characterised his rule”.
She said that former colonial power Britain, “as Zimbabwe’s oldest friend”, will do all it can to support free and fair elections and the rebuilding of the Zimbabwean economy.
Opposition leader Morgan Tsvangirai told the BBC he hoped that Zimbabwe was on a “new trajectory” that would include free and fair elections. He said Mr Mugabe should be allowed to “go and rest for his last days”.
US Embassy in Harare, the capital, said it was a “historic moment” and congratulated Zimbabweans who “raised their voices and stated peacefully and clearly that the time for change was overdue”
South Africa’s main opposition Democratic Alliance welcomed the move, saying Mr Mugabe had turned from “liberator to dictator”
Prominent Zimbabwean opposition politician David Coltart tweeted: “We have removed a tyrant but not yet a tyranny”
Civil society group the Platform for Concerned Citizens called for dialogue between all political parties, which it said should lead to the formation of a national transitional authority
Robert Mugabe won elections during his 37 years in power, but over the past 15 years these were marred by violence against political opponents.