When you have an infection, your body produces white blood cells to fight it. These affect your hypothalamus, the area of your brain that controls body temperature, causing you to heat up. In response your blood vessels tighten, causing your outer layer of skin to cool and your muscles to contract, making you shiver. Shivering produces more heat, raising your temperature again.
Later, the amount of heat you lose and make levels out and your body stays at a high temperature. When you’ve fought off the infection, your blood vessels open up and you sweat, cooling you down again.