So yes, many studies have shown that it is in fact possible to build muscle & lose fat at the same time . With an adequate stimulus & sufficient protein, muscle mass can be preserved & even built whilst in a deficit & losing fat.
This is most common in overweight individuals as, stored fat can act as a fuel supply for LBM growth even in the absence of a surplus. Also, it goes without saying that enhanced athletes also have the power to gain muscle & lose fat due to the impacts which anabolics have on muscle growth, irrespective of kcal intake . Finally, complete beginners have a better chance of gaining muscle & losing fat than advanced as, often the stimulus alone from the initiation of a new program is enough to “shock” the body & stimulate hypertrophy however, this is short lasted as you become more experienced . That being said, under the right circumstances (slow controlled weight loss & smart programming), even more experienced athletes can gain a muscle during a fat loss phase but again this is highly dependant on bf% ie. if absurdly lean and/or close to your genetic potential its less likely, if not then body recomp is possible
However, the gains you make in an kcal deficit will likely be far less than those made in a surplus as, though the stimulus for growth (training) is still present, kcals are not & they create an optimal environment for growth . Not saying that you can’t make gains in a deficit but if you’re seeking max gains then a surplus is almost necessary. Nonetheless hopefully that addressed the question of whether you can build muscle & lose fat at the same time. Yes you can but it depends.