Trick-or-treating is linked to multiple different ancestors whom vary with time. One major event is the Celtic Festival of Samhain, that marked the transition to the new year, and the beginning of the winter. Beliefs by the Celts, that the realms of the living and the dead overlapped and the spirits had the ability to roam the living realm, was major basis for the Halloween of the present day. The Celts also believed that to repel and confuse the spirits, impersonation with costumes of which appeared and looked like the souls was the solution. If they encountered a spirit during the feast, the costumed population were mistaken for spirits and left alone.
Trick-or-treating is an offshoot of souling, which started in the Middle Ages. Soulers would roam the neighborhood during Halloween and collect food or money in return for prayers that were said for the dead. As time, progressed food and treats were given in return for a small performance, like telling a joke or singing a song. The phrase is a suggestion that if a treat is given, then the child will not perform a “trick” on the owner of the house.
The custom of wearing costumes, masks, or other forms of disguise originated in Scotland. The Scottish children would roam the neighborhoods dressed as evil spirits, hoping the disguise would prevent evil spirits from doing harm by dressing like them. The children would traditionally carry a container, a lantern, basket, or bag, and at various homes asked for treats, such as cakes, fruit, and money.