The History of Carnival
To know the history of the yearly Carnival (Carnaval in Portuguese) you find all over the Catholic world, you would have to study European history.
The History of Carnival Starts With Greeks and Romans
The original roots are in ancient Greece where a spring celebration for the wine god Dionysos. This custom was taken over by the Romans who celebrated Bacchus (the Roman equivalent to Dionysos). In their fashion they introduced excessive amounts of food and drink as well as big orgies where slaves and masters exchanged clothes.
In Rome, the Catholic church took charge, but the yearly ceremony was continued. Now it became a last outlet before Lent, the forty days of fasting and abstinence before Easter. This week was very non-Christian and full of sex, intoxication and sin. The word is from Latin Carne Vale (In English: Farewell to the Flesh).
To The New World
Carnival spread to the new world and it was here it became this exciting mix of cultures that we have today. In New Orleans you have Mardi Gras and in Trinidad & Tobago you have the only real competitor to the Carnivals in Brazil.
In Rio de Janeiro the Carnival has been present since the mid 19th century, only around 1920 the unique music called Samba was incorporated. It was also then it became really popular with the masses. Samba is a style of music that have evolved in the slums of Rio, the favelas, and is a mix of influences from mainly Europe, Africa and South America.
A Mix of Cultures
What this feast is like in different parts of the world has obviously cultural explanations. You can understand a lot of the culture in Rio by understanding the history of Carnival: The party is from Greece, the dressing up is from Rome and the rhythm is from Africa and the frivolity from the Indians. Until the African influence became present the whole thing was a lot less intense and interesting, mainly because Catholic Europe had a quite strict environment.