  We study the origin and cosmic evolution of the mass-metallicity relation
(MZR) in star-forming galaxies based on a full, numerical chemical evolution
model. The model was designed to match the local MZRs for both gas and stars
simultaneously. This is achieved by invoking a time-dependent metal enrichment
process which assumes either a time-dependent metal outflow with larger metal
loading factors in galactic winds at early times, or a time-dependent Initial
Mass Function (IMF) with steeper slopes at early times. We compare the
predictions from this model with data sets covering redshifts 0<z<3.5. The data
suggests a two-phase evolution with a transition point around z ~ 1.5. Before
that epoch the MZRgas has been evolving parallel with no evolution in the
slope. After z ~ 1.5 the MZRgas started flattening until today. We show that
the predictions of both the variable metal outflow and the variable IMF model
match these observations very well. Our model also reproduces the evolution of
the main sequence, hence the correlation between galaxy mass and star formation
rate. We also compare the predicted redshift evolution of the MZRstar with data
from the literature. As the latter mostly contains data of massive, quenched
early-type galaxies, stellar metallicities at high redshifts tend to be higher
in the data than predicted by our model. Data of stellar metallicities of
lower-mass (< 10^11 solar mass), star-forming galaxies at high redshift is
required to test our model.
