By Karen Langley and Caitlin Ostroff

Stocks rose Wednesday as social unrest across the U.S. showed signs of calming and investors bet economic activity will improve with the ebbing of coronavirus infections and additional government stimulus.

Investors favored shares that have been battered during the market turmoil this year, suggesting their growing optimism about prospects for the economy.

The S&P 500 advanced 42.05 points, or 1.4%, to 3122.87. The Dow Jones Industrial Average added 527.24 points, or 2%, to 26269.89. The technology-heavy Nasdaq Composite gained 74.54 points, or 0.8%, to 9682.91, off only 1.4% from February's all-time high.

Stocks have staged a dramatic rally from their late March lows, a rebound most analysts attribute to the stimulus offered by the Federal Reserve and Congress to keep the economy afloat.

The S&P 500 was led higher Wednesday by the energy, financial and industrial groups, which have sustained the deepest losses in 2020 of the broad stock index's 11 sectors. All three group rose at least 3%.

Shares of small-cap companies, which tend to be sensitive to the economy, also rallied, with the Russell 2000 index gaining 2.4%.

"The fact that we're seeing broader participation right now is indicative of increasing risk appetite among investors and more confidence in the equity market rally," said Ed Campbell, portfolio manager at QMA.

Investors are looking ahead to the U.S. jobs report Friday for insight into the state of the labor market. In a potential bright sign Wednesday, the ADP National Employment Report showed nonfarm private sector employment in the U.S. decreased by 2.76 million jobs in May, a smaller loss than economists expected.

Many cities in the eastern U.S. remained largely quiet overnight, with the violent outbursts and skirmishes of recent days abating, though protesters defied curfews in some areas. Markets have continued to rally over the past week despite the social unrest in the U.S. as investors bet that the protests sparked by the killing of George Floyd wouldn't curtail business activity or have a sustained impact on the economy.

"Investors are continuing to ignore the three P's -- pandemic, protests and politics -- and are instead focused on what increasingly is being interpreted as a quicker and better than expected recovery in the economy and the idea that that may portend a quicker recovery for earnings as well," said David Donabedian, chief investment officer of CIBC Private Wealth Management.

Stimulus measures from governments and central banks in recent weeks have opened the floodgates on cheap money, which is making its way into financial markets and boosting asset prices, investors say. President Trump plans to meet with senior advisers as soon as this week to discuss policy options for the next coronavirus relief package as the administration prepares for negotiations with Congress, according to a senior administration official.

The gradual easing of lockdown measures around the world has failed to trigger a second wave of infections so far, fueling optimism in markets, said Patrick Spencer, managing director of U.S. investment firm Baird.

"There's little evidence of a resurgence in the virus, and that's really bolstered investor confidence," Mr. Spencer said. "If basically the fundamentals aren't as bad as the market discounts, then markets will always improve and the news is getting less bad."

Among individual stocks, shares of Lyft climbed $2.76, or 8.7%, to $34.44 after the ride-hailing platform said demand had risen in recent weeks as people began venturing out following Covid-19 lockdowns. CrowdStrike Holdings shares rose $5.85, or 6.3%, to $98.10 after the cybersecurity company raised its financial projections for the year.

Overseas, the Stoxx Europe 600 gained 2.5%. South Korea's Kospi Composite led gains in the Asia-Pacific region, adding 2.9% after the government proposed an extra budget worth $28.9 billion, the third this year, to ease the economic impact of the coronavirus pandemic.

Global equity markets are being fueled by an improvement in business sentiment, gradual reopenings, subsiding concerns about an oversupply of oil, and additional stimulus measures, according to Kerry Craig, global market strategist for J.P. Morgan Asset Management. However, the abundance of risks calls for a cautious approach to investing, he said.

"It's difficult to say what factors could impede the performance in equities, but we have tensions rising between the U.S. and China, a U.S. election coming up, and there is a politically charged environment in the U.S. right now, so there are enough risks out there," Mr. Craig said. "We'd rather be more balanced right now than rotate into cheaper parts of the market."

The yield on the 10-year U.S. Treasury note rose to 0.761%, from 0.679% Tuesday. Yields rise as bond prices fall.

Frances Yoon contributed to this article.

Write to Karen Langley at karen.langley@wsj.com and Caitlin Ostroff at caitlin.ostroff@wsj.com