We have investigated the mass-metallicity (M-Z) relation using galaxies at 0.4 < z <1.0 from the Gemini Deep Deep Survey (GDDS) and Canada-France Redshift Survey (CFRS). Deep K- and z-band photometry allowed us to measure stellar masses for 69 galaxies. From a subsample of 56 galaxies, for which metallicity of the interstellar medium is also measured, we identified a strong correlation between mass and metallicity for the first time in the distant universe. This was possible because of the larger baseline spanned by the sample in terms of metallicity (a factor of 7) and mass (a factor of 400) than in previous works. This correlation is much stronger and tighter than the luminosity-metallicity relation, confirming that stellar mass is a more meaningful physical parameter than luminosity. We find clear evidence for temporal evolution in the M-Z relation in the sense that at a given mass, a galaxy at z ∼ 0.7 tends to have lower metallicity than a local galaxy of similar mass. We use the z ∼ 0.1 Sloan Digital Sky Survey M-Z relation and a small sample of z ∼ 2.3 Lyman break galaxies with known mass and metallicity to propose an empirical redshift-dependent M-Z relation. According to this relation the stellar mass and metallicity in small galaxies evolve for a longer time than they do in massive galaxies. This relation predicts that the generally metal-poor damped Ly