Gas accretion as the origin of chemical abundance gradients in distant galaxies