Losing fat while gaining muscle is the holy grail of the fitness and body building world and it is not an easy task. Yes, it is possible in that your body will add muscle if you're working your muscles and getting enough protein. Running a calorie deficit is required to lose weight of course which makes it harder to get up the energy to work your muscles.
Usually when someone loses a lot of fat by running a calorie deficit for a long time they will also lose some lean body mass as well. Conversely a lot of strength training types will add on fat during a bulking (or fulking in extreme cases) phase in addition to lean body mass. Doing both at the same time requires a delicate balance.