There is an old quote that says “If you chase two birds at the same time, you will lose them both.”
Gaining muscle mass requires excess amount of calories over our daily maintenance.
We need extra amount of Proteins for providing enough amino acids to build muscle. But we also need extra amount of carbohydrates to fuel our workouts & lift heavier weights. Excess carbohydrates also causes insulin release which in turn helps in storing amino acids in our muscle tissues.
However losing fat requires just the opposite, we need to eat lesser calories than daily maintenance level so that our body taps into fat stores. We also need to maintain insulin level because insulin is a storage hormone and it not only stores amino acids but fatty acids as well.
However there can be a few exceptions.
1. You are a beginner. When you are just starting out your body would be more responsive to exercise induced stress and will add muscle mass faster. At this point you dont need to consume too many calories or protein. However as you get experienced your body adapts to the workout related stress and it becomes difficult to gain muscle mass easily.
2. You have strong Genetics: I have seen people who gain muscle mass and lose fat at the same time.
Some folks have strong nutrient partioning and their body is effective in storing nutrients in the muscles.
There are many professional athletes or bodybuilders who easily manage to lose fat and build muscle while doing competition prep and thus their body weight remains same but while muscle to fat ratio is changed significantly.
3. You just changed your lifestyle and eating habits.
If you change the type of foods you eat from junk to healthy you will lose fat and gain strength even if you are eating same calories. However this will not last longer and after certain point your body will adapt to your new routine.