I never knew either! I got my gelding at age ten and didn't clean his sheath until two years later
But yes it's gross, and it makes absolutely no difference whether he's stalled or on pasture. It's a buildup of natural oils and debris, it's going to occur no matter what. My gelding was on pasture his whole life and was still disgustingly dirty.
There's plenty of good articles to help get you through it.... and oddly enough, it's kind of satisfying once your done.
(thank god I have a mare now lol)