Mainz moved out of the automatic drop zone after winning their relegation six-pointer with Heidenheim.