Theory of Relaxation Oscillations in Optical Masers