There are all kinds of cases where calories in < calories out which could result in weight gain. For example, if you already have an excess of body fat and you are building muscle quickly, remember muscle has 4kcal/g while fat has 9. If you are building muscle 1-2x breaking down fat, you will gain weight while losing fat, and expending more calories than what you are taking in.
Again, it's an abstraction which mostly works but it is also clearly not entirely true.