The problem with the argument against calories in vs calories out when it comes to weight loss (not fat loss) is the alternative is a breach of thermodynamics, specifically that energy cannot be created or destroyed. Now obviously hydration level would mess up weight results somewhat, but if we put that to one side and assume equal hydration prior to your 12 week experiment as well as after, what we are left with is a position that is seemingly impossible (where you gain weight (aka energy) without taking that extra energy in). However obviously you have your results, so we have to look at alternatives (unless we assume your results are invalid which would be bad to do without looking into them further).
So the first possibility, as mentioned before, is that you were simply more hydrated - or at least retaining more water - than you were prior to the experiment, and this countered any weight loss you experienced from the caloric deficit.
The second possibility, as has been mentioned, is that you were either miscalculating your calories consumed or your energy expended. A lot of people do do this, but if you were weighing all your food out and checking calories in every time you ate anything, we know calories in is correct. Assuming none of the other possibilities I mention are plausible, the only logical result therefore is that your calorie expenditure dropped below your (extremely low) intake. Personally I’m sceptical of this as your intake was so low and changes in BMR are a) not rapid enough to have such an impact in 12 weeks, and b) not as severe as it would have to be for you to gain weight on such a low intake.
Finally, another possibility, though a particularly weird one, is that there was, in fact, no fat gain (despite the weight gain), but that fat was being burned for most calories due to the deficit, whilst muscle was being gained somehow which contributed to the weight gain (probably along with increased water retention). So how could this be? Well, it would depend on how muscly you were before the experiment and how much protein you were consuming amongst other things. I’m sceptical of this too as I’d be surprised if you were even meeting maintenance protein levels with your reported intake, but we’ll continue with this train of thought for now. As a rough approximation, fat contains 9 calories per gram and protein (ie muscle) contains 4 calories per gram. Now let’s assume each day you had a caloric deficit of 500 calories once BMR changes had occurred due to low intake. For ease, we’ll also assume this is all fat, though some would likely be glycogen and muscle. Anyway, that would be 55.555g of fat lost per day. It wouldn’t be completely impossible, though it would still be unlikely, that you could replenish 55.555g of muscle, or let’s say 75g due to your weight gain (which would be 300 calorie’s worth) whilst losing this amount of fat. So in the case above, you would be consuming a caloric deficit, but due to an extremely weird and very improbable set of circumstances, you would gain weight (though again, not fat) due to muscle gain. Note also that similar body composition changes can occur in response to changes in hormone levels, particularly demonstrated in individuals with thyroid issues
The one thing that is certain, though, is that you can not gain weight whilst maintaining a caloric deficit without a) increasing your water retention, b) having a weird redistribution of your body composition from fat to muscle (or glycogen), or c) you are subjected to a stronger gravitational pull. Assuming you didn’t lose fat, weren’t more hydrated, and weren’t subject to a stronger gravitational pull, you must have a higher caloric intake than your expenditure if you gain weight, or else you are breaching the laws of thermodynamics. If this were not the case, people with chronic anorexia would not look dangerously skinny - they’d simply look normal. In your case though, I suspect it was simply a case of increased water retention.