A possible origin of the mass-metallicity relation of galaxies