Based on your height and weight, and assuming a body fat % of 17% (Just assuming.. I have no idea what your BF % is, but it is relatively arbitrary here) then you have a Basal Metabolic Rate of ≈ 1340 calories per day. Your BMR is the number of calories your body burns simply by existing and handling normal bodily functions such as breathing, heartbeat, brain activity, etc. This does not include any exercise you do.
If you regularly only eat 300-500 calories, the reason you're gaining weight when you eat more than that is because your body is essentially operating in starvation mode most of the time. This happens when it doesn't get enough calories to sustain its normal functions. Your body essentially down-regulates the production of certain hormones and intentionally slows your metabolism to keep you from starving to death.
Simplified, when you are in a calorie deficit, your body has to break down some of its own tissues and convert them into usable energy to keep you going. This is the fundamental concept behind losing fat: the law of energy balance. If you take in more calories than you burn, you'll gain weight (either fat or muscle, depending on a variety of factors). If you take in the same as you burn, you'll maintain weight (but not always lean body mass, again depending on a variety of factors). If you take in less calories than you burn, then you'll lose weight (either fat or lean muscle tissue).
Reducing your calories causes you to lose fat, but reducing them too much makes your body think it's starving. When your body thinks its starving it kicks in many different survival mechanisms. If you're spending most of your time in starvation mode, when you do consume extra calories, your body will intentionally store them as fat after it meets its energy needs (which are now lowered due to the starvation response from your restricted calories).
The other problem is that when you restrict calories too much, your body will start to catabolize your lean muscle tissue to use for energy, and also to lower your body's caloric needs.
Your lean body mass is what primarily comprises your Basal Metabolic Rate. Simply put, the more lean muscle tissue you have, the more calories your body needs just to exist. So when your body catabolizes its muscle tissue, its essentially lowering your BMR, meaning your daily calorie requirements are lowered. That makes it much more likely that the next time you eat a large amount of calories you will be in a caloric surplus.
Lets take a look at an example of what typically happens when you restrict your calories too much:
Assuming 119 lbs @ 17% body fat, you have approximately 99 lbs. of lean body mass. You starve yourself for two weeks, only eating 300-500 calories per day. When you weigh yourself at the beginning of the following week, you weight 115 lbs, a loss of 4 lbs. But because your body is in starvation mode, a very large percentage of that weight is likely from lean muscle tissue. Assuming 50% is from LBM, your lean body mass is now only 97 lbs. Now say you go out and eat a large pizza or something and are in a caloric excess, you might find that you skyrocket back up to 119 lbs. But most of that weight is either water weight or fat. So now, your weight is the same as it was two weeks ago, but because you've lost lean muscle tissue, your body fat % is now closer to 18%.. which in gives you a lower daily caloric need even though you're the same weight.
This is an over simplification for explanation purposes, but this is the reason why you are experiencing this.
The best way to fix this problem is to start some resistance training (weights workouts) and to fix your eating habits. There will probably be a rough patch for the first week or two where your body gets used to the increased calories, and starts to gain weight. It is important to keep at it though. Another important thing to remember is that, especially if you're performing resistance training, you will be building lean muscle tissue as well. So keep in mind that an increase in scale weight does not always mean an identical increase in body fat.
Ideally, you'll want to figure out what your total daily energy expenditure (TDEE) is for your weight and body fat %, and accounting for your energy levels. In order to lose body fat while retaining lean muscle tissue, reduce your calorie intake by about 15-20% of your TDEE. This will allow your body to operate in a caloric deficit without triggering your body's survival mode.
Figuring out your TDEE is pretty easy if you know your current weight and your current body fat percentage. You can estimate your body fat and get a semi-close approximate of your TDEE.
Hope this makes sense and helps you.