Stocks aren’t quite immune to the virus, but they continue to overcome it.

For a third straight year, stock-fund investors pocketed big gains. The average diversified U.S.-stock fund rose 22.5% for the year, according to Refinitiv Lipper data—following gains of 19.1% in 2020 and 28.3% in the pre-pandemic year before that.

The stock-fund average...

Stocks aren’t quite immune to the virus, but they continue to overcome it.

For a third straight year, stock-fund investors pocketed big gains. The average diversified U.S.-stock fund rose 22.5% for the year, according to Refinitiv Lipper data—following gains of 19.1% in 2020 and 28.3% in the pre-pandemic year before that.

The stock-fund average gain included a 6.7% advance for the fourth quarter, as many investors remained convinced that Covid infections won’t lead to the same level of economic disruption as happened at the start of the pandemic and lockdowns in 2020.

International-stock funds were up 9.6% for 2021, including a 2.3% gain in the fourth quarter.

Underpinning the rally was an economic rebound that was stronger than many analysts had expected; companies posted some of their best-ever results.

Many investors nevertheless continued to hedge their bets. As they did in 2020, investors put more money into the relative safety of bond funds than they did into stock funds, foreign or domestic. They sent a net $587.10 billion into bond-focused mutual funds and exchange-traded funds in 2021, according to Investment Company Institute estimates. They put $113.07 billion into U.S.-stock funds and $193.19 billion into international-stock funds.

That caution, however, wasn’t rewarded. Bond funds declined in 2021. Funds tied to intermediate-maturity, investment-grade debt (the most common type of fixed-income fund) were off 1.3% for the year, including a 0.2% average decline in the fourth quarter.

The ride might be more rocky in 2022—at least according to some of the same analysts who underestimated 2021—with the Federal Reserve expected to raise interest rates, and the uncertainties of the virus and supply-chain problems.

Investors can expect 2022 to be a year of transition, says Lauren Goodwin,

economist and portfolio strategist at New York Life Investments. Although the world is going from “peak pandemic to managing Covid-19 and more-regular life,” she says, investment managers can expect greater volatility in markets, and lower returns than in past two years, as governments’ economic supports are reduced.

Mr. Power is a Wall Street Journal features editor in South Brunswick, N.J. Email him at william.power@wsj.com.