Carnivals had a religious background at first
The term “carnival” is generally assumed to be a Medieval-era Latin portmanteau that roughly means “say goodbye to the flesh” or “to remove the meat.” While that sounds like something you’d expect a spooky pale bald guy with pins in his skull to hiss at you after you unlock his accursed puzzle box, it’s a reference to the Catholic tradition of Lent.
During Lent, you are expected to give up meat and other vices until Easter. So, religious people, liking themselves some vice just like the rest of us, made the day before Lent begins into a feast of indulgence of everything you planned to give up. You may already recognize this as the backstory of Carnival in Brazil and of Mardi Gras. Most of our wildest, most anarchic holidays have roots in deeply religious Medieval peasants deciding they needed a handful of days a year to forget their lives as deeply religious Medieval peasants.