When the ventricles contract, atrioventricular valves close to prevent blood from flowing back into the atria. When the ventricles relax, semilunar valves close to prevent blood from flowing back into the ventricles.