From the Super Mario Wiki
Ads keep the MarioWiki independent and free :)
Mario's Cap is an important part of Mario's clothing, and one of the most iconic clothing items in gaming. The hat is red and has Mario's "M" symbol; he is almost never seen without it in his adventures.
When Mario collects almost any power-up, his cap will either disappear (as with the Frog, Tanooki, or Penguin Suits) or change appearance or color with the rest of his normal outfit (ex.: Fire Mario, Ice Mario, Rock Mario, Cloud Mario, etc.).
Mario was given his hat in his first appearance in Donkey Kong because of the limited advances in video game graphics when the game was made. A red cap was added because programmers found it difficult to create the hair movement that would occur when the character jumped.
Super Mario series
Super Mario 64 / Super Mario 64 DS
The hat has appeared with Mario ever since Donkey Kong, but was unimportant until Super Mario 64 - the first game in which Mario can lose his hat. If his hat is lost (having been stolen by a Ukiki or Klepto or blown off by a gust of wind, for instance), Mario takes more damage when attacked.
In the remake of Super Mario 64, Super Mario 64 DS, Toad reveals that the cap Mario wears is special, and that if it were to get blown off of Mario, he would have nothing but bad luck. As hinted in Super Mario 64 DS, Mario's Cap may be a source of his strength, though this does not apply in other games where Mario can have his hat off. If Mario loses his cap, he can get it back by going back to the course in which he lost it to find it. However, if he instead gets a Power Star from another course, he can simply talk to Toad (who will find and wear Mario's Cap) and get it back from him. Mario's Cap appears in the following courses, even when he is locked: Bob-omb Battlefield (Star 2 or 4), Whomp's Fortress (Star 1 or 2), Jolly Roger Bay, and Cool, Cool Mountain (Star 2). Mario's Cap can also be found in most levels when playing as Yoshi. Yoshi, Luigi, and Wario can pick up the cap, which gives them the appearance and abilities of Mario (though they retain their voices) until they lose it, get hurt, or finish a level.
Mario also makes use of three different types of caps in addition to his regular cap: the Wing Cap (which enables Mario to fly), the Metal Cap (which enables Mario to walk underwater and through fire), and the Vanish Cap (which enables Mario to walk through certain walls). While these powers are dispersed amongst Mario and his companions in Super Mario 64 DS, Mario's Cap gains a new ability: when worn, it turns Luigi, Yoshi, or Wario into Mario. If Mario loses his cap, he can't fly, use other caps, or use Power Flower abilities.
Due to certain glitches, it's possible for Mario to end up with more than one cap; the duplicate can be used as an effective weapon. In Super Mario 64 DS, if someone wearing Mario's Cap gets another cap, they get an extra life. However, if the real Mario gets another one of his caps from a certain glitch in Snowman's Land, he'll pick up the cap and nothing will happen.
Super Mario Sunshine
The same effects of Mario's Cap in Super Mario 64 hold true in Super Mario Sunshine, but with the added effect of Mario gradually losing health due to the heat. The only enemies that steal Mario's Cap in this game are Swipin' Stus.
Super Mario Galaxy 2
The cap's importance to Mario is stressed again at the end of Super Mario Galaxy 2, when Luma takes it with him as a souvenir before returning to the Comet Observatory with Rosalina, and Mario appears shocked to no longer have it. Following this, Mario is seen capless throughout the credits, but regains his cap when he is playable again, presumably getting a spare cap. The same thing happens when the player is Luigi.
Super Mario 3D Land
New Super Luigi U
Super Mario Odyssey
In Super Mario Odyssey, Mario's cap is a separate character called Cappy. Before Mario meets Cappy, his original hat can be seen being stomped on by Bowser, and Cappy uses the tarnished hat as a basis for what to turn into.
In Luigi's Mansion, Mario's Cap is one of the five items that Mario lost, and Luigi must bring it to Madame Clairvoya to receive information regarding Mario's whereabouts. However, Mario is seen wearing his cap throughout the entire game, even though Luigi finds it.
Mario Golf: World Tour / Mario Sports Superstars
Mario + Rabbids Kingdom Battle