By Rebecca Elliott

Drivers hitting the road over July Fourth weekend are poised to enjoy the cheapest holiday gasoline prices in years, but the gas stations are doing OK, too.

Fuel retailers have benefited from record margins on gasoline in recent months. The difference between the price U.S. consumers paid at the pump and the wholesale price of gasoline, less taxes and other costs, averaged about 41 cents a gallon during the first half of the year, the highest six-month average on record, according to analytics firm IHS Markit.

The average price at the pump in the U.S. for regular gasoline was about $2.18 a gallon on Friday, down around 21% from this time last year.

The wide margin between recent wholesale and pump prices has helped chains such as Alimentation Couche-Tard Inc., Murphy USA Inc. and Marathon Petroleum Corp.'s Speedway unit offset a steep decline in demand as people stayed home to slow the spread of the new coronavirus. U.S. gasoline consumption in April was more than a third below last year's levels, according to the Energy Information Administration.

"The sharp decline in volumes was mitigated by higher fuel margins, which benefited from the rapid and steep decline in crude prices during the quarter as well as lower competitive activity at retail," Brian Hannasch, Couche-Tard's chief executive, said.

The Canadian company, whose network of more than 14,000 stores spans more than a dozen countries and territories, reported $1.2 billion in gross profit from its fuels business during the quarter ended in April, a 50% increase from the same period a year earlier. In the U.S., Couche-Tard sold some 21% less fuel during the quarter but made more on each gallon. The company's U.S. margin averaged about 47 cents a gallon, up from around 19 cents during the same period a year ago.

Convenience stores often benefit when oil prices fall quickly in part because consumers typically are less price sensitive when gasoline is cheap, allowing retailers to take their time lowering prices. The opposite is true when oil prices are on the rise.

The price of U.S. benchmark oil fell by nearly $100 a barrel from the end of last year through April 20, when crude careered below zero for the first time. The average price of unleaded gasoline hit its low for the year about a week later: $1.77 a gallon, or 32% below year-end 2019 prices, according to IHS Markit. Gasoline margins ballooned in that time, peaking at nearly $1 a gallon in late March, the analytics firm's data shows.

Larger fuel margins in March and April helped Douglass Distributing Co.'s dozens of convenience shops make up for losses earlier in the year, even as demand in some stores fell by roughly half in late March. By early June, the North Texas fuel company's retail division was breaking even for the year, said Bill Douglass, who founded the firm.

But demand at Douglass's stores remained down around 14% last month, and the continued spread of the coronavirus throughout Texas makes forecasting for the second half of the year challenging.

"It's very difficult to plan because we don't know how deep this is going," Mr. Douglass said.

U.S. retail gasoline margins contracted to an average of about 27 cents a gallon as of Friday, according to IHS Markit, as oil has gotten more expensive. That compares with a July 3 average of around 19 cents a gallon over the prior five years.

With some 1,500 stores throughout the U.S., Murphy has the benefit of scale. Chief Executive Andrew Clyde said he expects the second half of the year to resemble June, when slightly higher-than-average margins more than offset lower demand. Early last month, Murphy sold an average of about 25% less fuel at its stores, but profits from its fuel division were up 15% year-over-year.

"The higher cost operators may struggle to maintain their prior level of profitability," Mr. Clyde said.

In North Dakota, margins at the Hub Convenience Stores are back below average after a strong spring. Year-to-date profits are down about 20% from a year ago, in part because the company has stepped up cleaning efforts at its six stores and installed sneeze guards, hand-sanitizing stations and automatic flushes in the bathrooms, Chief Executive Jared Scheeler said.

"We're spending more resources on both cleaning supplies and the corresponding labor for the extra cleaning that our consumers demand," Mr. Scheeler said.

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