We exploit the galaxy groups catalogue of Yang et al. and the galaxy properties measured in the Sloan Digital Sky Survey Data Releases 4 and 7 to study how the gas-phase metallicities of star-forming galaxies depend on environment. We find that satellite and central galaxies follow a qualitatively similar stellar mass (M★)–gas-phase metallicity relation, whereby their gas-phase metallicity increases with M★. Satellites, though, have higher gas-phase metallicities than equally massive centrals, and this difference increases with decreasing stellar mass. We find a maximum offset of 0.06 dex at log(M★/h−2 M⊙) ≃ 8.25. At fixed halo mass, centrals are more metal rich than satellites by ∼0.5 dex on average. This is simply due to the fact that, by definition, centrals are the most massive galaxies in their groups, and the fact that gas-phase metallicity increases with stellar mass. More interestingly, we also find that the gas-phase metallicity of satellites increases with halo mass (Mh) at fixed stellar mass. This increment is more pronounced for less massive galaxies, and, at M★ ≃ 109 h−2 M⊙, corresponds to ∼0.15 dex across the range 11 < log (Mh/h−1 M⊙) < 14. We also show that low-mass satellite galaxies have higher gas-phase metallicities than central galaxies of the same stellar metallicity. This difference becomes negligible for more massive galaxies of roughly solar metallicity. We demonstrate that the observed differences in gas-phase metallicity between centrals and satellites at fixed M★ are not a consequence of stellar mass stripping (advocated by Pasquali et al. in order to explain similar differences but in stellar metallicity), nor to the past star formation history of these galaxies as quantified by their surface mass density or gas mass fraction. Rather, we argue that these trends probably originate from a combination of three environmental effects: (i) strangulation, which prevents satellite galaxies from accreting new, low-metallicity gas which would otherwise dilute their interstellar medium; (ii) ram pressure stripping of the outer gas disc, thereby inhibiting radial inflows of low-metallicity gas and (iii) external pressure provided by the hot gas of the host halo which prevents metal-enriched outflows from escaping the galaxies. Each of these three mechanisms naturally explains why the difference in gas-phase metallicity between centrals and satellites increases with decreasing stellar mass and with increasing host halo mass, at least qualitatively. However, more detailed simulations and observations are required in order to discriminate between these mechanisms, and to test, in detail, whether they are consistent with the data.