Can I gain muscle while losing weight?
Yes and No.
The Yes side: It's easier and can be achievable for gym beginners, the genetically favoured (very low chance), the young (18–29), those who have previously had muscle in that region before (muscle memory) - all of course combined with the proper nutrition and right weight training.
The No side: All not in that previous group, so most people cannot. Of course the exception has to be steroids and other chemical compounds, which make your body gain muscle quicker than those with perfect genetics. You don't even need to exercise and will gain muscle. It's that powerful (but also dangerous).
My advice is to focus on either losing weight or gaining muscle. Don't tread the middle ground, for next to nothing will change. You need a caloric deficit to lose (fat) weight and a caloric surplus to gain (muscle) weight. If you don't exercise during weight loss, you will lose more muscle. That's not what you want, so you continue training for you not to lose muscle. If you are on a slight caloric surplus (good nutrition with enough protein) you need to train in order for that surplus to be mainly put to build and maintain muscle (more weight). Either way keep on training, because it will be good for lowering your body fat percentage. Most people recommend losing weight first, but you can go either way. Just don't bulk up for too long, because you may realize that a lot of your gains were just fat and you need to keep that in check once in a while to "keep it real" so that you know your actual progress.