BERLIN—Germany's government and energy utilities ended months of wrangling and agreed to extend the life of the country's nuclear-power plants, but popular resistance and possible legal challenges cloud the fate of the deal.

The plan to extend the life of Germany's 17 nuclear reactors is a major policy shift for Europe's biggest economy, which decided in 2000 under a center-left government to phase out nuclear power by 2022. Chancellor Angela Merkel's center-right government pledged to hang on to the reactors for longer when...