With subcultures this old no one can really tell.

I’ve heard plenty of theories:

That it got organized during the unification of Italy as a pro-Union rebel group that rioted and painted graffiti such as MAFIA (meaning “Mazzini Autorizza, Furti, Incendi e Ammazzamenti”) basically Mazzini, a pro-Union leader, Authorizes Theft, Arson and Murders.
Garibaldi, Mazzini and most of the House of Savoy where masons so the southern peasants mimicked those rituals.

Others trace it back to the arabic word mayhas which means “cocky” or “aggressive”.

More straightforward is the theory that implies that they started as goons (gabellotti) providing security to wealthy landowners.

And many more.