Researchers created a mathematical model of human metabolism, playing around with different formulas to see what dietary changes need to happen before weight gain occurs. What they discovered is that the body doesn't tally calories precisely as once believed. As long as an individual's long-term calorie intake remains constant, diets can vary by 600 calories (plus or minus) on a daily basis. In the study, small fluctuations of 600 calories led to "negligible weight gain" over a 10-year period.
Researchers believe that little to no weight gain occurs because healthy individuals usually "under-eat" one day and "over-eat" on another. So in the end, it all evens out. As long as you're eating healthy most of the time, having an occasional treat won't wreck your diet.