By Anna Hirtenstein and Michael Wursthorn

The S&P 500 edged higher Thursday, avoiding correction territory, as technology stocks recovered some of their recent losses.

Shares of Apple, Microsoft and other technology stocks notched gains, pulling the major stock indexes into the green. The advance ended a topsy-turvy session that had seen the benchmarks bob above and below the flatline several times following mixed economic data and an attempt by a senior Republican lawmaker to quell concerns around the elections.

The action underscored investors' ongoing lack of conviction in the stock market, suggesting a bumpy ride for major indexes amid a worsening coronavirus pandemic, worries about the economy and an increasingly contentious presidential election.

The S&P 500 closed up 9.67 points, or 0.3%, at 3246.59. The Dow Jones Industrial Average rose 52.31 points, or 0.2%, to 26815.44, while the tech-heavy Nasdaq Composite added 39.28 points, or 0.4%, to 10672.27. All three major indexes remain up at least 30% over the past six months.

The latest investor poll released Thursday by the American Association of Individual Investors showed nearly 46% of respondents expect stocks to fall over the next six months, up from 40% the prior week.

The bearish sentiment has made September a rough month. The S&P 500 was briefly on course for a correction Thursday morning, off more than 10% from its Sept. 2 closing record, following data showing hundreds of thousands of Americans continue to rely on jobless benefits. It was the latest evidence that the economic recovery from the coronavirus pandemic will be drawn-out.

David Lefkowitz, an executive director at UBS Global Wealth Management's chief investment office, said the S&P 500's near fall into a correction suggests investors are repricing stocks to account for the likelihood that lawmakers won't reach a deal on further fiscal stimulus, crimping an economic rebound.

The S&P 500 would need to close at or below 3222.76 to enter a correction.

"It's pretty unlikely we'll get anything," said Mr. Lefkowitz of investors' outlook.

Stocks made a U-turn and started climbing Thursday after the Commerce Department said home purchases reached a 14-year high, highlighting how the pandemic is reshaping life and economic activity.

The market also got a boost from reassurances from Senate Majority Leader Mitch McConnell via Twitter that the winner of November's presidential election would be inaugurated as planned in January. The tweet appeared to be a response to President Trump's refusal to commit to a peaceful transition of power a day earlier.

Investors tried to capitalize on the depressed prices of tech stocks, some analysts said. Apple shares, for example, remain 22% below their all-time high, while Microsoft's stock is off by 13%. Both stocked edged up more than 1% on the session.

Despite the volatility, big Wall Street banks like Wells Fargo and UBS remain upbeat on stocks over the long term, pinning their projections on a rebound in corporate earnings. Both banks are encouraging clients to pounce on the recent pullback.

Still, if Thursday's session is any indicator, daily gains may be painful as investors contend with a lengthy list of concerns. The coronavirus pandemic, which appears to be worsening in certain parts of the world, remains at the top of that list, said Mr. Lefkowitz, followed by uncertainty around an election conducted through mail-in voting.

"America sneezes and the rest of the world catches a cold: if you're being told that the world's largest economy will not recover without stimulus and they can't agree on a stimulus, then that has to be a negative piece of news," added Tony Yarrow, a multiasset fund manager at Wise Funds. "The mood among investors is extremely pessimistic at the moment."

Write to Anna Hirtenstein at anna.hirtenstein@wsj.com and Michael Wursthorn at Michael.Wursthorn@wsj.com