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, which has been in development since 2012, out of fears it would allow Moscow to increase its 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.

German Chancellor Angela Merkel met with President Biden last week during a farewell visit for the German leader.

Photo: alex edelman/pool/Shutterstock

Berlin and Washington as well would try to ensure that Ukraine continues to receive roughly $3 billion in annual transit fees that Russia pays under its current agreement with Kyiv, which runs through 2024. Officials didn’t explain how to ensure that Russia continues to make the payments.

The U.S. also would retain the prerogative of levying future pipeline sanctions in the case of actions deemed to represent Russian energy coercion, officials in Washington said.

State Department spokesman Ned Price on Tuesday noted that Mr. Biden told German Chancellor Angela Merkel during a White House visit last week that “we continue to oppose the Nord Stream 2 pipeline,” while adding that the project was 90% complete when Mr. Biden took office and that officials didn’t think it could be stopped through sanctions.

German officials said they rejected a U.S. demand to include a so-called kill-switch clause in the pipeline’s operating rules. This would have enabled Berlin to suspend gas flows if Russia were to make aggressive moves toward its neighbors or Western allies. German negotiators argued that such state interference in a privately owned project could be the target of a legal challenge.

Berlin agreed instead not to reject future sanctions against the Russian energy sector, officials said.

The agreement between the Biden administration and Germany was described by officials in Berlin and Washington and by a congressional official familiar with the issue. Details of the agreement began circulating Monday evening, following a Reuters report on a possible deal.

Kyiv sees the existing gas-transit network in Ukraine and the revenue it brings as a check against Russia, especially after Moscow seized the Ukrainian region of Crimea and fomented rebellion in the country’s east in 2014. U.S. supporters of Ukraine have opposed any agreement permitting the construction of the pipeline.

“Any deal that allows for the completion of the Nord Stream 2 pipeline is badly flawed,” said Pat Toomey (R., Pa.), the ranking member of the Senate Banking Committee. Mr. Toomey urged the administration to impose sanctions “to halt Moscow’s efforts to weaponize the supply of gas in Europe.”

A focus of the forthcoming deal is the promotion of Ukrainian energy independence, said one of the people familiar with the talks, adding that the agreement ensures that Russia “cannot use energy as a coercive tool against Ukraine or any nation.”

Despite the U.S. sanctions waiver in May, this person said the U.S. would continue to examine entities involved in the project and will make clear “companies risk sanctions if they are involved in Nord Stream 2.”

Derek Chollet, a senior State Department policy adviser, is in Kyiv this week along with a German counterpart to inform Ukrainian President Volodymyr Zelensky of the agreement’s details. Mr. Chollet is scheduled for a similar trip to Poland, which has vehemently opposed the pipeline.

Word of the deal has rankled Ukrainian officials, who view the existing pipeline network in their country as a rare point of leverage against Russia in the two countries’ seven-year-old conflict. Last week, Mr. Putin delivered another in a series of polemics challenging Ukrainian sovereignty. Rendering the existing pipeline superfluous could offer Russia a freer military hand on Ukrainian territory, Ukraine advocates fear.

White House press secretary Jen Psaki said Tuesday that Mr. Biden told Ms. Merkel last week “that we have ongoing concerns about how the project threatens European energy security, undermines Ukraine’s security and the security of our…allies and partners.”

Ms. Psaki said Mr. Biden directed staffers to work jointly with German counterparts to complete an agreement.

An announcement of the deal was delayed to avoid eclipsing Ms. Merkel’s farewell visit to the White House, according to officials close to the negotiations. Ms. Merkel has made the completion of the pipeline, which could occur later this year, a signal achievement marking the close of her political career, people familiar with her thinking said.

Write to Bojan Pancevski at bojan.pancevski@wsj.com and Brett Forrest at brett.forrest@wsj.com