Aims. By means of chemical evolution models for ellipticals, spirals, and irregular galaxies, we aim at investigating the physical meaning and the redshift evolution of the mass-metallicity relation, as well as how this relation is connected with galaxy morphology. Methods. Our models distinguish among different morphological types through the use of different infall, outflow, and star formation prescriptions. We assume that galaxy morphologies do not change with cosmic time. We present a method accounting for a spread in the epochs of galaxy formation and refining the galactic mass grid. To do that, we extracted the formation times randomly and assumed an age dispersion Δ t . We compared our predictions to observational results obtained for galaxies between redshifts 0.07 and 3.5. Results. We reproduce the mass-metallicity (MZ) relation mainly by means of an increasing efficiency of star formation with mass in galaxies of all morphological types, without any need to invokegalactic outflows favoring the loss of metals in the less massive galaxies. Our predictions can help constraining the slope and the zero point of the observed local MZ relation, both affected by uncertainties related to the use of different metallicity calibrations. We show how, by considering the MZ, the O/H vs. star formation rate (SFR), and the SFR vs. galactic mass diagrams at various redshifts, it is possible to constrain the morphology of the galaxies producing these relations. Our results indicate that the galaxies observed at z = 3.5 should be mainly proto-ellipticals, whereas at z = 2.2 the observed galaxies consist of a morphological mix of proto-spirals and proto-ellipticals. At lower redshifts, the observed MZ relation is reproduced by considering both spirals and irregulars. Galaxies with different star formation histories may overlap in the MZ diagram, but measures of abundance ratios such as [O/Fe] can help to break this degeneracy. Predictions for the MZ relations for other elements (C, N, Mg, Si, Fe) are also presented, with largest dispersions predicted for elements produced in considerable amounts by Type Ia SNe, owing to the long lifetimes of their progenitors.