Kaiserslautern and Magdeburg were involved in a five-goal thriller as the visitors secured a valuable three points at the bottom.