No. When you lose weight, you lose fat and muscle. Anyone that tells you otherwise, is lying. You can take steps to help protect muscle loss, but you can’t prevent it. Weight training will help preserve muscle as much as possible, but you will still lose muscle. You also have to consume large amounts of protein to maintain your muscles during weight loss, and you will still lose some muscle.
The same is true when you gain weight. When you gain weight, you usually gain fat. Unless you are weight training and eating lots of protein, you will gain mostly fat.
Yo-yo dieting loses muscle and fat, but adds back fat when you gain the weight back. Our bodies become very efficient at storing fat. See the yo-yo dieting section for a more detailed discussion.