Of course you could gain weight, if you took in more calories than you expended. See, that's it, really, it's calories. Calories are a unit of heat. If you take in fewer than you burn, you lose weight - so you can lose weight either through dieting or exercise. If you take in more calories than you burn (either through overeating or by being sedentary), you gain weight.
Where fat comes into the picture is threefold:
(1) fat tends to not contain much else, so you tend to not get vitamins, fiber, etc. when you ingest fat. So you're taking in calories but aren't garnering any benefits other than being able to burn stuff. Since you need to intake Vitamin C, etc., anyway, you are intaking those calories plus the calories from other items, such as fats.
(2) fat requires far fewer calories to metabolize. That is, to convert it to either energy or, if you've got enough energy for your needs, fat. So if you intake an equal amount of calories from fat or from protein, you end up with fewer usable calories from the protein simply because the Krebs cycle in your body needs more fuel in order to store the protein than it does the fat.
(3) anything can be converted into fat, although sugar is the least likely because it tends to be converted directly to brain fuel. This isn't to say that pure sugar won't make you fat - it will (and it will rot your teeth and may contribute to acne and make your blood sugar rise and fall very quickly, which can be a problem if you are hypoglycemic or diabetic) - but sugar first heads to the brain. But most of us eat too much of just about everything, so even excess sugars end up as fat. And once it's converted to fat, it's fat, regardless of the original source of the calories.
One other thing - building muscle is helpful not only because the same amount of muscle poundage is more densely-packed than the same amount of fat poundage (e. g. you can fit into a smaller size), but also because maintaining muscle mass requires some calories. So when your body % skews higher towards the muscle end, you need more calories to maintain your body weight. Therefore, you can intake more calories without gaining weight.
Here's a site that talks about calories and weight:
http://www.wvda.org/calcs/