If I gain weight and exercise in between, will I lose the weight I gained or just build lean body mass?

It depends. As you exercise, you build muscle. Muscle weighs more than fat, so you may gain weight but nevertheless be healthier. As you consume more calories than you burn, you gain weight. But if you do not exercise, you will gain fat weight, not muscle weight. What the scale says is not as important as how you feel and how much muscle you have compared to fat.