Mario's first appearance in a video game was in 1981's Donkey Kong. We now know of Donkey Kong as a star of his own games, but in the original Donkey Kong game, Donkey Kong was the villain who kidnapped a damsel in distress. Mario, whose name was "Jumpman" back then, had to rescue the damsel by jumping over barrels that Donkey Kong would throw at him.
Back then, not only did Mario have a different name, but a different job as well. "Jumpman" was originally supposed to be a carpenter. Also, Mario's signature cap and mustache were added in because it made him easier to draw. The original Donkey Kong didn't have very good graphics, and it was easier for Mario (Jumpman) to have a hat and a mustache than have hair and a mouth.
(source: 1, 2)