Recent studies have found higher galaxy metallicities in richer environments. It is not yet clear, however, whether metallicity-environment dependencies are merely an indirect consequence of environmentally dependent formation histories, or of environment related processes directly affecting metallicity. Here, we present a first detailed study of metallicity-environment correlations in a cosmological hydrodynamical simulation, in particular the Illustris simulation. Illustris galaxies display similar relations to those observed. Utilizing our knowledge of simulated formation histories, and leveraging the large simulation volume, we construct galaxy samples of satellites and centrals that are matched in formation histories. This allows us to find that ∼ 1/3 of the metallicity-environment correlation is due to different formation histories in different environments. This is a combined effect of satellites (in particular, in denser environments) having on average lower z = 0 star formation rates (SFRs), and of their older stellar ages, even at a given z = 0 SFR. Most of the difference, ∼ 2/3, however, is caused by the higher concentration of star-forming disks of satellite galaxies, as this biases their SFR-weighted metallicities toward their inner, more metal-rich parts. With a newly defined quantity, the 'radially averaged' metallicity, which captures the metallicity profile but is independent of the SFR profile, the metallicities of satellites and centrals become environmentally independent once they are matched in formation history. We find that circumgalactic metallicity (defined as rapidly inflowing gas around the virial radius), while sensitive to environment, has no measurable effect on the metallicity of the star-forming gas inside the galaxies.