We use a sample of 87 rest-frame ultraviolet-selected star-forming galaxies with mean spectroscopic redshift \langle z \rangle = 2.26 \pm 0.17 to study the correlation between metallicity and stellar mass at high redshift . Using stellar masses determined from spectral energy distribution fitting to U _ { n } G { \cal R } JK _ { s } ( and Spitzer IRAC , for 37 % of the sample ) photometry , we divide the sample into six bins in stellar mass , and construct six composite H \alpha + [ N ii ] spectra from all of the objects in each bin . We estimate the mean oxygen abundance in each bin from the [ N ii ] / H \alpha ratio , and find a monotonic increase in metallicity with increasing stellar mass , from 12 + log ( O / H ) < 8.2 for galaxies with \langle M _ { \star } \rangle = 2.7 \times 10 ^ { 9 } M _ { \odot } to 12 + log ( O / H ) = 8.6 for galaxies with \langle M _ { \star } \rangle = 1.0 \times 10 ^ { 11 } M _ { \odot } . The mass-metallicity relation at z \sim 2 is offset from the local mass-metallicity relation by \sim 0.3 dex , in the sense that galaxies of a given stellar mass have lower metallicity at high redshift . A corresponding metallicity-luminosity relation constructed by binning the galaxies according to rest-frame B magnitude shows no significant correlation . This lack of correlation is explained by the known large variation in the rest-frame optical mass-to-light ratio at z \sim 2 , and indicates that the correlation with stellar mass is more fundamental . We use the empirical relation between star formation rate density and gas density to estimate the gas fractions of the galaxies , finding an increase in gas fraction with decreasing stellar mass . The median gas fraction is more than two times higher than that found in local star-forming galaxies , providing a natural explanation for the lower metallicities of the z \sim 2 galaxies . These gas fractions combined with the observed metallicities allow the estimation of the effective yield y _ { eff } as a function of stellar mass ; in contrast to observations in the local universe which show a decrease in y _ { eff } with decreasing baryonic mass , we find a slight increase . Such a variation of metallicity with gas fraction is best fit by a model with supersolar yield and an outflow rate \sim 4 times higher than the star formation rate . We conclude that the mass-metallicity relation at high redshift is driven by the increase in metallicity as the gas fraction decreases through star formation , and is likely modulated by metal loss from strong outflows in galaxies of all masses . Our ability to detect differential metal loss as a function of mass is limited by the small range of baryonic masses spanned by the galaxies in the sample , but there is no evidence for preferential loss of metals from low mass galaxies as has been suggested in the local universe .