The Origin of the Relation between Metallicity and Size in Star-forming Galaxies