The U.S. and Germany have reached an agreement allowing the completion of a controversial Russian natural-gas pipeline, according to officials from Berlin and Washington, who expect to announce the deal as soon as Wednesday, bringing an end to years of tension between the two allies.

The Biden administration will effectively waive Washington’s longstanding opposition to the pipeline, Nord Stream 2, a change in the U.S. stance, ending years of speculation over the fate of the project, which has come to dominate European energy-sector forecasts. Germany under the agreement will agree to assist Ukraine in energy-related projects and diplomacy.

U.S. officials under the previous two presidential administrations opposed Nord Stream 2, out of fears it would heighten Moscow’s economic and political sway across Europe. The pipeline would allow the Kremlin to increase European dependence on its natural gas, then use it to blackmail U.S. allies, critics have said, charges Russia has dismissed.

President Biden, seeking closer ties with Europe and with Berlin in particular, waived U.S. sanctions against the Swiss-registered Russian pipeline firm, Nord Stream 2 AG, and its chief executive in May, signaling a change in the U.S. stance.

Mr. Biden continues to oppose the pipeline and views it as a Kremlin move to expand its influence over others, officials said Tuesday, but considers a united group of allies to be the most effective way to counter Moscow.

For Russia, the U.S.-Germany deal means it will be able to double the volume of natural gas exported directly to Germany via the pipeline beneath the Baltic Sea, while bypassing an existing route through Ukraine.

One person familiar with the talks said the deal was close to conclusion and expected in coming days. Another person familiar with the talks said the deal could be announced as early as Wednesday.

Under the four-point agreement, Germany and the U.S. would invest $50 million in Ukrainian green-tech infrastructure, encompassing renewable energy and related industries. Germany also would support energy talks in the Three Seas Initiative, a Central European diplomatic forum.