Shares of big technology companies have been flying past the rest of the market.

After languishing for months, the group has staged a resurgence with several tech behemoths hitting records in recent sessions.

Apple Inc. shares closed at a record Friday, after notching their first new high since January earlier in the week. Microsoft Corp. and Amazon.com Inc. shares also jumped to records Wednesday and Thursday, respectively.

“I’m very bullish on companies that can provide strong organic growth and predictable growth,” said Dev Kantesaria, founder of Valley Forge Capital, who said he recently added to his Amazon holdings. “Certainly the big technology companies will be able to do that.”

Some investors and analysts say they have become less optimistic that the U.S. economy’s recovery will sustain its strong momentum. Lackluster economic data over the past week and fears about the Delta variant of the Covid-19 virus have stoked volatility in stock and bond markets.

Eight tech heavyweights accounted for more than half of the S&P 500’s 7.3% gain from May 12, when the index hit a near-term low, through early July, according to a Bespoke Investment Group note Thursday. Apple, Amazon, Alphabet Inc., Facebook Inc., Microsoft, Netflix Inc., Nvidia Corp. , and Tesla Inc. pushed the broader index to new highs.

The strong showing for tech marks a sharp shift from earlier in the year. For many months, investors had fled the government bond market and scooped up shares of companies that would prosper if the U.S. economy continued its rapid recovery from the Covid-19 pandemic. Energy companies and shares of big banks and other financial firms soared as Treasury yields edged higher.

Those wagers have crumbled in recent sessions, highlighting the turbulence among stocks that has been a staple of this year, despite an ascendant market.

The tech sector’s recent win marks a revival of a trade that flourished in 2020, helping drive the S&P 500’s recovery for much of last year.

Anchoring the run for growth companies is dwindling bond yields. Investors have stampeded into Treasurys, sending the yield on the 10-year Treasury note to the lowest level since February as bond prices have rallied. Many individual investors have also piled in, snapping up exchange-traded funds that track bonds, according to JPMorgan Chase & Co. strategists.

The low yields have boosted the outlook for tech and growth bets that investors think could keep delivering strong results during a sluggish stretch for the broader economy, reviving a trade that worked for much of last year, and even the last decade.

“The 10-year—which seems to be the one thing that’s driving how people are positioned in the market—has defied all odds and predictions. It’s led a rotation,” said John Porter, chief investment officer of equities at Mellon Investments. “When you look at the broader indices, it’s relatively smooth sailing. Underneath the surface there have been wild changes.”

Goldman Sachs analysts recently said they expect slowing economic growth to buoy growth stocks in the second half of the year, though the sector will remain volatile. The firm expects the economic growth rate to peak in the third quarter and decelerate later in 2021.

In the coming week, investors will be parsing data on inflation as well as earnings from banks like JPMorgan Chase & Co. and Goldman Sachs Group Inc. for more clues on the recovery, as well as the path of cyclical stocks like banks.

SHARE YOUR THOUGHTS

How long do you think the boom in tech stocks will last? Join the conversation below.

Rising interest rates can be problematic for growth and tech firms, in part because their earnings are expected to come later in the future. Rising yields make the current value of earnings more attractive relative to future ones.

Some investors say that buying shares of so-called value companies like energy and financial firms hasn’t run its course yet, and that those groups will rally. But many investors are struggling to position for the next phase of the recovery. The recent shift marks a test case for a stock market that has already staged a strong rally in the first half of the year—with the S&P 500 hitting 38 highs in 2021—fueled by the prospect of a robust economic recovery.

“I think it means that gains in the market are going to be harder to come by,” said Eddie Perkin, chief investment officer of equities at Eaton Vance.

Write to Gunjan Banerji at Gunjan.Banerji@wsj.com