What I don't understand though from a science point of view is how is it possible to not lose any weight or to gain weight when you are in a caloric deficit?
The thing is, if you're gaining, you're not in a deficit. You may be consuming a pitifully small and dangerously inadequate amount of food, but your body is burning even less than that in an attempt to survive. I kept thinking the answer to that was to eat even less, but the answer was to eat more and let my body feel safe to burn more. At the height of my restriction, I would gain on typical "diet" calorie recommendations. Now, after years of adequate fuel, I need 2-3 times that to keep from losing. So, it was about eating enough to let my metabolism and hormones recover. Then I began to gradually lose on significantly more food. Your natural weight is where you settle when you eat without restriction. For me that was quite a bit higher than when I was anorexic, but also quite a bit lower than the initial recovery weight I feared I'd be stuck at forever.
Totally agree with Skwigg that the "magic" seems to happen after a long time at maintenance. When I had hypothalamic amenorrhea I was forced to eat a maintenance and chill on the exercise front to get my fertility back and "heal my metabolism". It definitely took at least 2 years for me to be "healed". And during that time I was a higher weight than I was comfortable with, but which really wasn't high at all. I'm 5'4" and those two years of recovery I fluctuated between 130 and 140lbs. Not at all huge, but higher weights than I felt comfortable with. But then the "magic" happened and, like Skwigg, I found myself able to eat more than I ever had or could during those years of restriction. And a word of caution, you can mess everything up again. I have after each one of my pregnancies and it's taken me at least a year to fix the (physical) issues again. The mental side is just as hard.