If you were anaemic, the GP wouldn't tell you to take 5mg of iron filings every day, he'd prescribe ferrous sulphate in a dose which would give you 50mg of iron a day. Which you would take, and get constipated, nauseated and have black poo. So you'd come to MN, and we'd tell you to take ferrous bisglycinate, at a dose which would give you 25mg of iron a day. Which you would take, have no side-effects, and your anaemia would recover faster than it would on ferrous sulphate, despite the lower dose. This is because not all forms of iron are the same. Your body absorbs and processes them differently.
It is very similar with calories. The calorie content of food is measured by burning it under controlled conditions, and measuring the amount of energy it generates. (Because not all foods are tested, the amount if energy they can generate by being burned is calculated from the known values of their components.)
But - hey! newsflash! - you are not a machine for burning fuel under controlled conditions. You are a complex biological process. Just as not all forms of iron are the same, not all calories are the same. Your response to the calorie may depend upon the form it takes, your ability to digest that form, your hormone levels, your response to those hormones, many many factors.
This is why all calories are not the same, and calories in do not necessarily have to equal calories out in order to have stable weight.