For a proper comparison, you need to pay up the guy paying off the mortgage with someone who eg invests similar amounts in an index fund.
To be even more proper, you'd need to allow the stock market investor to use leverage, just like the guy with the mortgage does.
Or, if you want to avoid the mortgage/leverage complication, we can compare someone buying a house outright with someone investing the same amount in stocks, and uses the returns to pay rent.
> Around here, houses appreciate in value. Quite significantly, actually. A house can gain 100k in value in just a few years.
Total returns on eg the S&P500 over the last few decades have been pretty good, too. And it's a much more diversified and liquid investment than a single house in a single location.
For some people in some places, buying a house might be better than buying stocks, for some others it might be worse. Results also depend on taxes and jurisdiction and personal preferences. But it's not automatic that buying a house is better than renting.