Some of the hottest stocks in the U.S. are pointing to an economic cool-down.

Utilities and healthcare are among the best-performing groups in the S&P 500 so far this quarter, with gains of 7.8% and 6.6%, respectively, compared with a 4.9% rise in the broad stock index. Big winners include utility NextEra Energy Inc., which is up 14% this quarter, while shares of medical company Danaher Corp. are up 19%.

The gains are noteworthy because investors typically pile into those types of stocks when they are expecting the outlook to darken. Visits to the doctor and electricity use are less apt to decline in a pinch than spending on vacations or new furniture. Goldman Sachs this month cut its third-quarter U.S. economic growth forecast to 5.5% from 9%, citing slowing consumer spending in the face of renewed Covid-19 outbreaks.

A drop in economic growth from its mid-2021 pace hardly presages a recession, which is the development that often spells the end of stocks’ advance. But rallies in these so-called defensive sectors can presage broader market retreats, investors say, potentially spelling out a fresh test for a market whose post-pandemic rise has surprised many stock-market bulls.

“When those sectors are doing well, as they’re doing now, that tells you that the market is bracing for either a slowdown in the economy or some sort of a correction in the broad market,” said Phil Orlando, chief equity-market strategist at asset manager Federated Hermes.

The market has powered to new highs through the pandemic, bolstered by aggressive central-bank interventions, government stimulus and a vigorous earnings recovery. The U.S. economy exceeded its pre-pandemic size in the second quarter of 2021, with S&P 500 companies’ profits rising 92% from a year earlier, according to analyst projections on FactSet.

But as Americans approach the second autumn of the pandemic with no end in sight, questions about the markets’ resilience are coming to the fore. The S&P 500 has advanced 20% this year and set 52 record closes—its highest number of records in a calendar year through the end of August, according to Dow Jones Market Data. Valuations have edged lower this year as earnings soared but remain at historically high levels.

And the S&P 500 hasn’t experienced a 5% pullback since October, the longest such respite since a stretch from June 2016 to early February 2018. When the market has declined, investors have seen it as a buying opportunity.

“Economic and earnings growth will likely be very good for the third quarter, but less than the second and maybe a little less than was expected,” said Bob Doll, chief investment officer at Crossmark Global Investments, which manages about $5.8 billion.

Crossmark in recent months bought shares of utilities and healthcare stocks and trimmed positions in consumer-discretionary and materials companies, he said.

The shift in sentiment has helped propel stocks such as utility American Water Works Co. , healthcare operator HCA Healthcare Inc. and grocery chain Kroger Co. to double-digit percentage gains in July and August.

SHARE YOUR THOUGHTS

Are you making any investments in defensive stocks? How do they fit into your portfolio? Join the conversation below.

Healthcare stocks have relatively attractive valuations, some investors say. The sector traded late last week at about 18 times its projected earnings over the next 12 months, compared with about 21 times for the S&P 500, according to FactSet.

The utilities group, meanwhile, traded at 20 times projected earnings, a more modest discount to the broad market, but boasts a dividend yield of 3%—more than double that of the S&P 500.

The recovery since last year’s market low has been marked by sharp rotations among favored groups of stocks. The energy sector was once hot. Now, it has lost ground this quarter, dropping 9.4% as oil prices retreated.

Early in the year, expectations for a powerful economic rebound helped propel value stocks to their largest lead over technology and other growth stocks in two decades. Then, the spread of the Delta variant and lackluster economic data caused some investors to second-guess those wagers, and big tech once again moved to the top of the market.

Now, the economy is continuing to grow but at a slower pace than some had anticipated, leading to the move into defensive holdings.

“I think the market is going to continue to plow forward,” said Stephanie Lang, chief investment officer at wealth-management firm Homrich Berg. “Investors are going to continue to come in and buy the dip.”

Write to Karen Langley at karen.langley@wsj.com