On Halloween, Irish peasants would beg the rich for food. For those that refused, they would play a practical joke. So, in an effort to avoid being tricked, the rich would hand out cookies, candy, and fruit – a practice that morphed into trick-or-treating today.
(source)