According to Wikipedia, the origin of this festival goes like this:
In 1945, during a parade of gigantes y cabezudos (roughly translates to “Giants and Big-Heads”), young men who wanted to participate in the event staged a brawl in the town's main square, the Plaza del Pueblo. Since there was a vegetable stand nearby, they picked up tomatoes and used them as weapons. The police had to intervene to break up the fight, and forced those responsible to pay the damages incurred.
The following year the young people repeated the fight on the same Wednesday of August, only this time they brought their own tomatoes from home. They were again dispersed by the police. After repeating this in subsequent years, the party was established. In 1950, the town allowed the tomato hurling to take place, however the next year it was again stopped. A lot of young people were imprisoned but the Buñol residents forced the authorities to let them go. The festival gained popularity with more and participants getting involved every year. After subsequent years it was banned again with threats of serious penalties. In the year 1957, some young people planned to celebrate "the tomato's funeral", with singers, musicians, and comedies. The main attraction however, was the coffin with a big tomato inside being carried around by youth and a band playing the funeral marches. Considering this popularity of the festival and the alarming demand, 1957 saw the festival becoming official with certain rules and restrictions..
According to La Tomatina’s official web site, there are five simple rules that must be followed.
1. You must not bring bottles or other type of objects which could cause an accident.
2. You must not tear or throw t-shirts.
3. The tomatoes must be squashed before throwing them, to avoid hurting people.
4. You must be careful of any lorry (truck or van).
5. When you hear the second shot, you must stop throwing tomatoes.
And this is how the festival begins:
At around 11 a .m., the first event of the Tomatina begins. A ham is placed upon a cockaigne pole (a large, greased pole), and the tomato fight can begin only when someone is able to climb to the top and bring it down. People struggle against each other, climbing atop one another, in order to be the one to pull down the ham. With this victory, the tomato-throwing begins.
Many trucks haul the bounty of tomatoes into the center of the town, Plaza del Pueblo. The tomatoes come from Extremadura, where they are less expensive. The signal for the beginning of the fight is firing of the cannon, and the chaos begins. Once it begins, the battle is generally every man for himself. Somewhere between an hour and two hours, the fighting ends and the cannon is fired once more to signal the end. At this point no more tomatoes can be thrown. The cleaning process involves the use of fire trucks to spray down the streets, with water provided from a Roman aqueduct.
A little nutty, perhaps, and very messy, but it sounds like a lot of fun!