The origins of trick-or-treating dates back to the Celtic festival called Samhain, which was celebrated on October 31. These people thought the dead returned to earth on that day, so they celebrated by lighting fires and paying respect to the deceased.
Also on the celebration of Samhain, people dressed in animal skins to try to scare away spirits, while food was set out to please the same phantom visitors, according to History.com. This tradition dated back to the Middle Ages with a custom called "mumming."
Centuries later, young people of Irish and Scottish heritage took part in acts called guising where they dressed up in costumes. They went door-to-door requesting offerings after they performed a trick, which was, in essence, telling a joke, reading a poem or singing a song.
When Irish and Scottish people arrived in America, they brought along the same traditions as their ancestors. It was common for young people to destroy property during this guising on Halloween in the 1920s. To help curtail this sort of damage, organized trick-or-treating hours became commonplace in the United States in the 1930s, that is, until World War II broke out and there was a sugar shortage.
After the war, the tradition continued. The concept of "trick-or-treating" appeared in print in 1951 in a Peanuts cartoon, and then the phrase appeared in 1952 in a cartoon starring Donald Duck and his three nephews. The title of the cartoon was "Trick or Treat."