The lungs is the place where exchange of CO2 for O2 takes place. When you do heavy excersize, you will consume a lot of O2 and produce a lot of CO2. If you don't breathe in and out often enough, gas exchange will not take place efficiently since the lungs will become saturated with CO2 quicker. CO2 saturation in the blood sends a signal to the brain to take deeper and faster breathes.
Taking deep, fast breathes when this is not necessary will lead to a drop of CO2 levels in the blood, leading to a feeling of anxiety, which causes you to take even faster breathes, leading to hyperventilation. Breathing in a bag will cause you to breathe in more CO2, bringing blood CO2 levels to a normal level, so that hyperventilation will stop.
I don't know if this counts.. but when you breathe out CO2, the blood pH buffer system uses protons to make bicarbonate. This effectively decreases your blood pH. This is important because when you do rapid exercise, lactic acid production in the muscles may give you acidosis.