Trick-or-treating—setting off on Halloween night in costume and ringing doorbells to demand treats—has been a tradition in the United States and other countries for more than a century. Its origins remain murky but traces can be identified in ancient Celtic festivals, early Roman Catholic holidays, medieval practices—and even British politics.

READ MORE: Origins, Traditions, and Costumes of Halloween

Ancient Origins of Trick-or-Treating

A man representing the Winter King holds a flaming sword as he takes part in a ceremony celebrating Samhain in Somerset, England, 2017. Matt Cardy/Getty Images

Halloween has its roots in the ancient, pre-Christian Celtic festival of Samhain, which was celebrated on the night of October 31. The Celts, who lived 2,000 years ago in the area that is now Ireland, the United Kingdom and northern France, believed that the dead returned to earth on Samhain. On the sacred night, people gathered to light bonfires, offer sacrifices and pay homage to the dead.

Did you know? Although it is unknown precisely where and when the phrase “trick or treat” was coined, the custom had been firmly established in American popular culture by 1951, when trick-or-treating was depicted in the Peanuts comic strip. In 1952, Disney produced a cartoon called “Trick or Treat” featuring Donald Duck and his nephews Huey, Dewey and Louie.

During some Celtic celebrations of Samhain, villagers disguised themselves in costumes made of animal skins to drive away phantom visitors; banquet tables were prepared and food was left out to placate unwelcome spirits.

In later centuries, people began dressing as ghosts, demons and other malevolent creatures, performing antics in exchange for food and drink. This custom, known as mumming, dates back to the Middle Ages and is thought to be an antecedent of trick-or-treating.

Early Christian and Medieval Roots of Trick-or-Treating

An early 20th-century postcard of children on Halloween. Rykoff Collection/Corbis/Getty Images

By the ninth century, Christianity had spread into Celtic lands, where it gradually blended with and supplanted older pagan rites. In 1000 A.D. the church designated November 2 as All Souls’ Day, a time for honoring the dead. Celebrations in England resembled Celtic commemorations of Samhain, complete with bonfires and masquerades.

Poor people would visit the houses of wealthier families and receive pastries called soul cakes in exchange for a promise to pray for the souls of the homeowners’ dead relatives. Known as "souling," the practice was later taken up by children, who would go from door to door asking for gifts such as food, money and ale.

In Scotland and Ireland, young people took part in a tradition called guising, dressing up in costume and accepting offerings from various households. Rather than pledging to pray for the dead, they would sing a song, recite a poem, tell a joke or perform another sort of “trick” before collecting their treat, which typically consisted of fruit, nuts or coins.

READ MORE: Who Invented Candy Corn?

Guy Fawkes Night Celebrations

An effigy of Guy Fawkes is burnt on Bonfire Night, 1952. Bert Hardy/Picture Post/Hulton Archive/Getty Images

Modern-day trick-or-treating also has elements akin to annual celebrations of Guy Fawkes Night (also known as Bonfire Night). On this night, which commemorates the foiling of the Gunpowder Plot in 1605, British children wore masks and carry effigies while begging for pennies. On November 5, 1606, Fawkes was executed for his role in the Catholic-led conspiracy to blow up England’s parliament building and remove King James I, a Protestant, from power.

On the original Guy Fawkes Day, celebrated immediately after the famous plotter’s execution, communal bonfires, or “bone fires,” were lit to burn effigies and the symbolic “bones” of the Catholic pope. By the early 19th century, children bearing effigies of Fawkes were roaming the streets on the evening of November 5, asking for “a penny for the Guy.”

Trick-or-Treating in the United States

Children dressed up for Halloween in Jersey City, NJ. Bettmann Archive/Getty Images

Some American colonists celebrated Guy Fawkes Day, and in the mid-19th century, large numbers of new immigrants, especially those fleeing the Irish Potato Famine in the 1840s, helped popularize Halloween.

In the early 20th century, Irish and Scottish communities revived the Old World traditions of souling and guising in the United States. By the 1920s, however, pranks had become the Halloween activity of choice for rowdy young people.

The Great Depression exacerbated the problem, with Halloween mischief often devolving into vandalism, physical assaults and sporadic acts of violence. One theory suggests that excessive pranks on Halloween led to the widespread adoption of an organized, community-based trick-or-treating tradition in the 1930s. This trend was abruptly curtailed, however, with the outbreak of World War II, when sugar rationing meant there were few treats to hand out. At the height of the postwar baby boom, trick-or-treating reclaimed its place among other Halloween customs. It quickly became standard practice for millions of children in America’s cities and newly built suburbs. No longer constrained by sugar rationing, candy companies capitalized on the lucrative ritual, launching national advertising campaigns specifically aimed at Halloween.

Today, Americans spend an estimated $2.6 billion on candy on Halloween, according to the National Retail Federation, and the day, itself, has become the nation’s second-largest commercial holiday.

READ MORE: Halloween Was Once So Dangerous That Some Cities Considered Banning It