By Rebecca Elliott

Fuel maker Marathon Petroleum Corp. said it has agreed to sell its gas stations to the owners of the 7-Eleven convenience store chain for $21 billion in the largest U.S. energy-related deal of the year.

The all-cash agreement with 7-Eleven Inc. comes less than a year after Marathon agreed to spin off its convenience-store chain, known as Speedway, under pressure from activist investors including Elliott Management Corp.

"This was the chance of a lifetime," said Ryuichi Isaka, president of Seven & I Holdings Co., the Tokyo-based parent of 7-Eleven. He said he saw convenience stores in the U.S. as the biggest growth driver for the company, whose department stores and supermarkets in Japan are struggling.

Findlay, Ohio-based Marathon had been close to a deal with Seven & I earlier this year, but talks fell apart in March as the coronavirus pandemic took hold. The company revived sales discussions months later, The Wall Street Journal reported in June. Other suitors included Canada-based convenience-store chain Alimentation Couche-Tard Inc.

Marathon Chief Executive Michael Hennigan said the deal "crystalizes the significant value of the Speedway business" and "delivers on our commitment to unlock the value of our assets."

The Speedway deal includes about 3,900 convenience stores and would bring 7-Eleven's retail footprint in the U.S. and Canada to around 14,000 locations. Under the agreement, expected to close early next year, Marathon would supply 7-Eleven with about 7.7 billion gallons of fuel per year for 15 years.

"This acquisition is the largest in our company's history and will allow us to continue to grow and diversify our presence in the U.S., particularly in the Midwest and East Coast," 7-Eleven CEO Joe DePinto said in a statement.

There has been a flurry of energy deals in recent weeks during what has otherwise been a slow year for oil-and-gas-related acquisitions. Last month, Chevron Corp. agreed to buy Noble Energy Inc. for about $5 billion, and Berkshire Hathaway Inc. inked a deal to buy Dominion Energy Inc.'s natural-gas storage and transmission network for $4 billion, excluding debt.

Marathon's sale of its gas-station chain would provide a cash infusion for a company that, like other U.S. refiners, has been badly bruised by a decline in fuel demand because of the pandemic. It told employees Friday that it has no plans to restart two refineries that it idled this spring.

The company said it expects the deal to generate about $16.5 billion in after-tax proceeds, which it plans to use to pare debt and return capital to shareholders.

Seven & I said it expected the acquisition to produce synergies worth $475 million to $575 million over three years. Mr. Isaka, who formerly led the convenience-store business in Japan, said the company has been trying to transform its U.S. outlets to focus more on fresh food and private brand products.

Mr. Isaka said convenience-store sales have been rising during the coronavirus pandemic. As a result of the acquisition, about one-third of Seven & I's operating profit is expected to come from North America. In the year ended February 2020, Seven & I reported sales of Yen6.6 trillion ($62 billion) and operating profit of Yen424 billion ($4 billion).

Marathon, which reports second-quarter earnings Monday, posted a $9.2 billion first-quarter loss, its largest on record, in May as it took $12.4 billion in charges. As of Friday, the company's shares had lost more than a third of their value since the end of last year, slightly more than the decline in U.S. benchmark oil prices.

Elliott has repeatedly called on Marathon to split into three businesses -- a gas-station chain, a pipeline business and a fuel-making operation -- saying the integrated model left the company undervalued.

Marathon acquiesced in part last fall, saying that it would spin off Speedway and that Gary Heminger, the company's chief executive at the time, would step down. Elliott didn't immediately respond to a request for comment Sunday evening.

Megumi Fujikawa in Tokyo contributed to this article.

Write to Rebecca Elliott at rebecca.elliott@wsj.com

Corrections & Amplifications Berkshire Hathaway inked a deal to buy Dominion Energy Inc.'s natural-gas storage and transmission network. An earlier version of this article incorrectly described the company as Dominion Energy Corp. (Corrected on Aug. 2)