We simulate cosmic chemical enrichment with a hydrodynamical model including supernova and hypernova feedback. We find that the majority of stars in present-day massive galaxies formed in much smaller galaxies at high redshifts, despite their late assembly times. The hypernova feedback drives galactic outflows efficiently in low mass galaxies, and these winds eject heavy elements into the intergalactic medium. The ejected baryon fraction is larger for less massive galaxies, correlates well with stellar metallicity. The observed mass-metallicity relation is well reproduced as a result of the mass-dependent galactic winds. We also predict the cosmic supernova and gamma-ray burst rate histories.