Stocks traded in a narrow range, with the major indexes closing out the quarter at or near record highs following a June rally powered by technology stocks.

The S&P 500 rose 5.7 points, or 0.1%, on Wednesday to close at 4297.5, marking the benchmark index’s 34th record close of the year. The index is now up 14.4% in 2021, representing the largest first-half gain in a year since 2019.

Meanwhile, the Dow Jones Industrial Average added 210.22 points, or 0.6%, to finish at 34502.51. The tech-heavy Nasdaq Composite edged back from Tuesday’s record close, declining 24.381 points, or 0.2%, to 14503.95. The index still rose for the fifth consecutive quarter.

Recently, investors have been rotating back into the technology stocks that they favored during Covid-19 lockdowns after inflation fears eased and low bond yields spurred a hunt for better returns. Optimism about the economic recovery, the prospect of more fiscal stimulus and confidence that the Federal Reserve will continue to support credit markets have also boosted value over growth stocks in recent days. The last day of the month and quarter also generally leads to portfolio reshuffling.

“This market is an environment that looks very attractive because we have all this pent-up demand,” said Kristina Hooper, chief global market strategist at Invesco. “This all suggests that the economic rebound will be powerful in the back half of this year.”

The S&P’s consumer staples and discretionary sectors finished the trading day up 0.72% and less than 0.1%, respectively, after Tuesday’s data from the Conference Board showed its index of consumer confidence rose in June, beating analysts’ expectations.

“Consumer discretionary has been a laggard,” Ms. Hooper said. “Clearly, the consumers are coming back and we saw very positive consumer sentiment, which is going to be a part of this economic reopening.”

Shares of Gap increased $1.07, or about 3.3%, to close at $33.65 but were still down 1% so far this week. Didi, the Beijing-based ride-sharing company, gained $0.140, or 1%, to finish at $14.14 on the first day of trading after its IPO. Didi’s American depositary shares opened trading at $16.65 on Wednesday afternoon, 19% above their $14 IPO price, though the rally faded into the afternoon.

Driving Wednesday’s rally were energy and industrial services, where Cabot Oil & Gas rose $1.18, or about 7.3%, to close at $17.46.

“We’ve had a really strong quarter,” said Jerry Braakman, chief investment officer of First American Trust in Santa Ana, Calif. “I think the real question becomes what’s going to happen in the second half.’”

The Dow Jones Industrial Average declined 0.1% in June, marking its weakest month since January and ending a four-month winning streak.

The nonfarm private sector in the U.S. added 692,000 jobs in June, a drop from the previous month, but still above economists’ estimates, according to Wednesday’s ADP National Employment Report. Investors are closely scrutinizing any new information on the strength of the labor market, which the Fed has indicated is a priority.

In bond markets, the yield on the benchmark 10-year Treasury note edged down to 1.443% from 1.479%. The yield has dropped 0.149 percentage point this month. Bond yields fall when prices rise.

Brent crude, the international benchmark for oil prices, rose 0.5% to about $75.13 a barrel ahead of a meeting this week of major oil producers to discuss a potential increase in supply. The gauge is on course to post its biggest first-half gain since 2009, having risen nearly 45% so far in 2021.

Overseas, the pan-continental Stoxx Europe 600 slid 0.8%.

In Asia, major benchmarks ended the final day of trading in June on a mixed note. The Shanghai Composite Index advanced 0.5% Wednesday, while Hong Kong’s Hang Seng Index slipped 0.6% by the close of trading.

The Nasdaq Composite and S&P 500 ended Tuesday at records.

The Nasdaq Composite and S&P 500 ended Tuesday at records.

Photo: Richard Drew/Associated Press

Write to Anna Hirtenstein at anna.hirtenstein@wsj.com