Walmart Inc., like many of the families that shop in its stores, is getting squeezed by higher food prices and other rising costs.

The country’s largest retailer by revenue said sales increased in the most recent quarter, but higher product, supply-chain and employee costs ate into profits, sending the retailer’s stock sharply lower Tuesday.

Throughout the pandemic, Walmart and other big-box retailers have benefited from higher demand for a host of goods and while some costs have increased, profits have grown along with sales. On Tuesday Walmart said its net income in the April-ended quarter fell 25% from a year ago, and that earnings per share came in below analysts’ forecasts.

“There were some things that happened during the quarter that were different than we expected. We are trying to be transparent about those things,” said Walmart Chief Executive Doug McMillon on a conference call with analysts, acknowledging a rare earnings miss at the company. “Our first quarter is a disappointment to us. We are going to put it behind us and have a strong year.”

Walmart shares fell 9% in early Tuesday trading to about $135, though the stock had outperformed the broader market this year. The shares were up about 2% year to date before the earnings report, compared with a roughly 16% decline for the S&P 500.

U.S. shoppers increased retail spending in April for the fourth straight month as inflation held close to its highest level in four decades, according to government data released Tuesday. The retail spending figures include restaurants, gas stations and car dealerships.

Higher food costs prompted shoppers to shift their spending away from general merchandise, eating into profits, Walmart executives said. Some consumers are still making big-ticket purchases, they said, while sales of store-brand goods are rising, a sign that other shoppers are trying to save money.

“We are helping a family that’s at the lower end of the income scale to be able to afford to feed their families during this inflationary time,” said Mr. McMillon. Walmart aims to undercut competitors on certain frequent purchases, such as bread, milk, canned tuna and proteins, he said. “Given that stimulus checks happened last year, there was some benefit to some of those folks that is eroding over time.”

Also Tuesday, Home Depot Inc. said consumers are still spending on home renovations despite higher prices on everything from appliances to paint. The retailer said it had fewer shoppers in its stores to start the spring season but they spent more per trip. The company raised its profit and revenue forecasts for the year.

Walmart cited a host of cost increases that ate into profits during the quarter.

Walmart overstaffed stores for a few weeks as workers came back from Covid-19 leave faster than expected, executives said on a call with analysts. That issue was resolved, primarily through attrition, said Mr. McMillon. Price increases ate into profits as Walmart aims to keep prices lower than competitors in an inflationary environment. Executives indicated some prices would rise and that negotiations with suppliers would help balance those costs.

Inventory levels also rose too fast, said executives, causing Walmart to put more goods on sale.

“We’re not happy with the profit performance for the quarter and we’ve taken action, especially in the latter part of the quarter on cost negotiations, staffing levels and pricing, while also managing our price gaps,” Mr. McMillon said on Tuesday.

Inventory levels increased over 33% in the quarter from the same period last year. That rise reflects the higher cost of goods due to inflation, the company said, along with Walmart’s choice to buy products aggressively amid supply-chain snarls and rising demand for some goods in past quarters. Product markdowns, when a retailer sells an item at a discount, were $100 million more than expected in the quarter.

Supply-chain costs also came in higher than expected as the war in Ukraine and uptick in Covid-19 globally created delays, said Chief Financial Officer Brett Biggs. “The supply chain didn’t move towards normal as quickly as we thought,” he said.

Net income was $2.05 billion in the latest quarter. Adjusted earnings per share were $1.30, missing the consensus forecast from analysts of about $1.48, according to FactSet. Overall revenue was up 2.4% to $141.57 billion.

U.S. comparable sales, those from stores or digital channels operating for at least 12 months, rose 3% in the quarter ended April 29, led by strength in food categories, the company said Tuesday when it reported earnings. The number of U.S. transactions stayed flat during the quarter, while the amount spent each time rose 3%, the company said.

Walmart said it expects U.S. comparable sales for the full year to grow about 3.5%, up from a prior estimate of 3%. It expects operating income to decrease about 1%, excluding currency fluctuations, down from a previous estimate of an around 3% increase.

Online buying behavior has shifted back to prepandemic levels, pressuring sales at online retailers such as Wayfair Inc. and Amazon.com Inc. On Tuesday Walmart said U.S. e-commerce sales rose 1% in the most recent quarter, compared with a 37% increase in the same quarter last year. Net sales within Amazon’s online stores segment—the company’s largest—fell 3% in the first quarter from a year earlier.

Write to Sarah Nassauer at sarah.nassauer@wsj.com