Chancellor Angela Merkel said the decision, hammered out by her centre-right coalition overnight, marked the start of a "fundamental" rethink of energy policy in the world's number four economy.
"We want the electricity of the future to be safer and at the same time reliable and affordable," Merkel told reporters as she accepted the findings of an expert commission on nuclear power she appointed in March in response to the crisis at Japan's Fukushima plant.
"That means we must have a new approach to the supply network, energy efficiency, renewable energy and also long-term monitoring of the process," she said.
The commission found that it would be viable within a decade for Germany to mothball all 17 of its nuclear reactors, eight of which are currently off the electricity grid.
Environment Minister Norbert Röttgen announced the decision by the government in the early hours of Monday morning, describing it as "irreversible."
"This decision is consistent, decisive and clear," he said.
Most of Germany's 17 will be shut down by 2021, though if the transition to other forms of power proves difficult, three of the newest reactors can be kept online until 2022.
Germany's seven oldest reactors, plus Krümmel reactor – all of which are currently offline after Japan's Fukushima disaster – will be closed down permanently. However, one of these, yet to be named, will remain on stand-by from 2013 – to be switched on only in the event of electricity shortages – until the full phase out in 2021 or 2022.
Philippsburg 1 and Biblis B reactors have been mentioned as candidates. Keeping a reactor on stand-by could cost up to €50 million per year, news magazine Der Spiegel reported.
Monday's decision made Germany the first major industrial power to announce plans to give up atomic energy entirely.
But it also means that the country will have to find the 22 percent of its electricity needs currently covered by nuclear reactors from another source.
Röttgen insisted there was no danger of blackouts.
"We assure that the electricity supply will be ensured at all times and for all users," he pledged, but did not provide details.
The decision is effectively a return to the timetable set by the previous Social Democrat-Green coalition government a decade ago. And it is a humbling U-turn for Merkel, who at the end of 2010 decided to extend the lifetime of Germany's 17 reactors by an average of 12 years, which would have kept them open until the mid-2030s.
That decision was unpopular in Germany even before the earthquake and tsunami in March that severely damaged the Fukushima nuclear facility in Japan, prompting Merkel's review of nuclear policy.
Her zig-zagging on what has been a highly emotive issue in the country since the 1970s has cost her since at the ballot box.
Merkel herself has blamed the Fukushima nuclear disaster for recent defeats in state elections.
In the latest, on May 23, the anti-nuclear Greens pushed her conservative party into third place in a vote in the northern state of Bremen, the first time they had scored more votes than the conservatives in a regional or federal election.
The late-night wrangling in Merkel's fractious team saw the pro-business Free Democrats (FDP) arguing against a fixed end date for nuclear power, and to maintain two reserve reactors in case of energy shortages.
FDP parliamentary leader Rainer Brüderle defended the plan for a reserve plant from 2013, saying it was needed to guarantee a reliable supply of electricity. It was not about keeping a backdoor open but simply about avoiding blackouts.
He told broadcaster ZDF on Monday morning that the hole in the electricity supply leading from the nuclear shutdown would have to be filled by building more natural gas power plants. The share of renewable energy would need to rise from the current 17 percent to 35 percent by 2020.
He ruled out imports of atomic energy from other countries.
Environmental group Greenpeace slammed the plan as “absolutely unacceptable” and accused Merkel of breaking her word.
The agreement did not constitute an exit from nuclear power as quickly as possible, as Merkel had previously promised, Greenpeace nuclear expert Tobias Münchmeyer said on Monday.
“Merkel has broken her word and learned nothing from Fukushima,” he said.
During the late-night negotiations, the Christian Social Union, the Bavarian sister party of Merkel's Christian Democrats, fought for an exit within 10 years.
Some coalition members had called for a built-in review clause which could have seen the decision revisited, but this was thrown out in the final round of negotiations.
Röttgen said the government had largely followed the recommendations of an "ethics panel" appointed by Merkel after the Fukushima disaster, which called for an end to nuclear power in Germany within a decade.
Greens parliamentary leader Jürgen Trittin told broadcaster Bayerischer Rundfunk that the plan contained a “back door” for atomic power.
It included “in a so-far unverifiable measure the possibility to transfer power from one atomic power station to another, and in this way to include an extension” in the plan, he said.
However the general plan to phase out nuclear power by 2022 was “a step in the right direction,” he said.