Taxation of capital gains is built around the notion of realization. Your wealth increases as your assets grow in value, but these are only paper profits — unrealized gains — until you dispose of the assets in a sale or exchange. Stock investments receive normal treatment under this rule.
Owners of appreciated real estate can postpone tax on gains when they work out an exchange for another real estate investment. By going through some hocus pocus (highly artificial but entirely legal), they can make what amounts to a sale of one property and purchase of another, rather than a genuine exchange.
The real question isn’t why stock owners can’t do something similar, but rather why the tax law provides this special treatment for real estate investments. Various reasons are offered, but I suspect the answer has more to do with the lobbying power of the real estate investment industry than tax policy.