The Celts thought they had to protect themselves from ghosts and goblins.
If the jack-o-lantern didn't scare off the spirits, then they had their gift of food (a treat).
If that didn't work - there was another plan... a disguise.
If you wore a scary mask or disguise the ghosts hopefully wouldn't pick on you.
That is how wearing costumes started.