Many here are saying you need to eat a surplus, which isn't true for most people.
Also, I don't think you need to eat enough to gain fat.
Building muscle takes more calories than keeping the same amount which takes more than losing.
If you lift hard, you'll need extra calories to heal that you don't need to stay the same. You can even measure the amount as the after burn effect. You could in theory measure the increased calorie needs after an injury.
If your body is using those calories to improve itself is it really a surplus? If some are turning into fat for sure that is a surplus.
I'm fairly confident that the idea of metabolic rate being fixed is BS. Your body can turn up or turn down the burn. Any 3 or 4 day fast will make you cold because it turned the fire down. If you gained fat you were maxed out and still had too much or the wrong food or some kind of metabolic syndrome.