Part of the difference is that when you buy pre-installed RAM you're paying for a service as well as a component. When you buy it afterwards, you're buying just the component and doing the service yourself.
It's been this way since the earliest days of PCs, from any manufacturer. Manufacturers offer the convenience of out-of-box configuration, and can charge a premium for that.
You might as well ask why auto dealerships charge so much for parts like oil filters and batteries, when a person handy with tools could buy the parts at a lower price and do the work themselves.
Parts suppliers are competing for the business of people whose primary motivation is to save money. If they charge too high a price for their parts, the time and effort that goes into do-it-yourself repairs makes less sense, and they sell fewer parts. Manufacturers and repair shops are providing a service to people with different motivations - convenience, lack of knowledge/skill, lack of time, peace of mind, warranty coverage.... And as with anything else in this life, they're able to charge whatever the market will bear.