By Annie Gasparro

Kraft Heinz Co. wrote down the value of Oscar Mayer, Maxwell House and several other of its well-known brands, reflecting the challenges for the food maker despite strong sales in recent months amid the pandemic.

Kraft Heinz on Thursday recorded $2.9 billion in impairment charges, which resulted in the company swinging to a loss in the second quarter. The hit comes after Kraft Heinz last year reduced the value of its assets by nearly $17 billion. It said at the time that consumers had gravitated toward niche brands they viewed as fresher, healthier, or more innovative.

The company said $655 million of the latest charges were related to its U.S. food service business, which could have slower sales growth going forward as the pandemic slashes sales at restaurants, schools and hotels.

When a company finds that the value of an asset is lower than it has been accounting for on its balance sheet, it reflects the loss through a write-down on its earnings. If the asset loses all its value or is removed from the balance sheet, it records a write-off.

Still, Kraft Heinz Chief Executive Miguel Patricio said the pandemic has reinvigorated sales as shoppers return to big brands they know well. He expects that will help accelerate the company's yearslong turnaround effort.

"We have to keep these consumers with us," Mr. Patricio said on a call with investors.

Kraft Heinz's results topped analyst expectations for quarter when excluding the impact of the charges. Its shares fell 2% Thursday to $34.85.

The company said its comparable sales -- -- which strips out the effects of currency fluctuations, acquisitions and divestitures -- -- rose 7.4% in the second quarter. "This is not to say that we captured 100% of the opportunity. There are some categories where we have lost share, and we are working hard to fix that," Mr. Patricio said.

Kellogg Co. also reported better-than-expected earnings Thursday, with comparable sales up 9% in the second quarter. Kellogg's shares rose 1% to $71.75.

Food makers initially saw an unprecedented surge in sales in March when consumers filled up their pantries and refrigerators amid lockdowns to curtail the spread of the coronavirus.

Snack maker Mondelez International Inc. earlier this week reported strong sales growth in North American, saying its biggest brands such as Oreo cookies have gained market share.

"Consumers have been going back to the brands they know and trust," Mondelez CEO Dirk Van de Put said.

Grocery sales data indicate that consumers aren't stocking up or hoarding food anymore, but they are still buying more at stores and less at restaurants. Nestlé SA said Thursday sales slowed recently, though the maker of Lean Cuisine and DiGiorno pizza reported comparable sales were up 1.3% in the second quarter.

Several big food makers, including Kraft Heinz, have said they are increasing their marketing spending in hopes of keeping their sales momentum going beyond the pandemic. "We are developing better perspectives on where consumers are going," Mr. Patricio said.

Kraft Heinz reported a second-quarter loss of $1.65 billion, or $1.35 a share, compared with a profit of $449 million, or 37 cents a share, the year earlier.

The company said excluding the impairment charges and after other adjustments, it earned a profit of 80 cents a share, 15 cents more than forecasts from analysts. Overall revenue in the quarter ended June 27 rose to $6.65 billion from $6.41 billion for the second quarter last year and was ahead of the consensus estimate of $6.55 billion.

Micah Maidenberg contributed to this article.

Write to Annie Gasparro at annie.gasparro@wsj.com