It’s because airlines price based on demand not costs and they optimize for maximum revenue. When you buy a product in a store, the store typically prices the item with an X% markup and that’s it. If there is an increase in demand for that product they don’t raise prices. If there is a decrease in demand they don’t drop prices.

This is a pretty good explanation of it: https://crankyflier.com/2012/01/03/why-does-a-connecting-flight-cost-more-than-a-nonstop-ask-cranky/