Coevolution of metallicity and star formation in galaxies to z ≃ 3.7 - II. A theoretical model