Historians link trick-or-treating to a few different ancestors, some old and some new. One is the Celtic festival of Samhain, which marked the transition to the new year, and the end of the harvest and beginning of the winter. The ancient Celts believed that during this short window (October 31 to November 2 in our modern calendar), the realms of the living and the dead overlapped and that spirits both good and bad could walk among the living. To confuse and ward off the evil spirits, the Celts would sometimes impersonate them with costumes of white clothing and masks or blackface. If they encountered a spirit during the feast, the costumed Celts would be mistaken for spirits and left alone.

As Christianity gained influence in the British Isles, the old Pagan customs were Christianized and adapted to help ease the Celts’ conversion. Three Christian holidays—All Hallows’ Eve, All Saints’ Day and All Souls’ Day, together known as Hallowmas—were placed on the same days as Samhain. All Hallow’s Eve eventually got shortened to Hallowe'en, and then Halloween, in conversation and casual usage.

Going around the neighborhood for goodies may be an offshoot of souling, which started in the Middle Ages, also in the British Isles. Soulers, mostly children and some poor adults, would go to local homes during Hallowmas and collect food or money in return for prayers said for the dead on All Souls’ Day. A secular version of souling, called guising, eventually sprang up and is first recorded in Scotland in the 19th century. Guisers went door to door and earned food treats or money by offering a small performance, like telling a joke or singing a song. Some accounts of both these traditions make note of “fantastic costumes” that borrowed both from Samhain and British mummery. (They also mention soulers and guisers carrying vegetable lanterns, precursors to the jack-o’-lantern.)

In Trick of Treat: A History of Halloween, horror author and Halloween historian Lisa Norton argues that, rather than old British customs, trick-or-treating is rooted in a more modern, more American practice with no ties to the usual ghouls and ghosts. Belsnickling, derived from the German mumming tradition of Peltznickel, was a Christmastime tradition in German-American communities where children would dress in costume and then call on their neighbors to see if the adults could guess the identities of the disguised guests. In one version of the practice, the children were rewarded with food or other treats if no one could identify them. “This same custom appears in some early descriptions of trick or treat,” Norton writes, “lending credence to the possibility that it derived from its Christmas cousin.”

Whether it was born of guising or belsnickling, trick-or-treating emerged from the ethnic enclaves as its own, wholly North American custom in the early 20th century. In 1927, a newspaper in Alberta makes the first recorded use of “trick or treat” (“The youthful tormentors were at back door and front demanding edible plunder by the word 'trick or treat' to which the inmates gladly responded and sent the robbers away rejoicing”), and the term and the practice spread throughout the 1930s. After a lull caused by WWII sugar rationing, trick-or-treating surged in popularity in the 1950s, and became enshrined in pop culture with appearances in national media like The Jack Benny Show and Peanuts comic strips.