Aims. We analyse the distribution of matter around the progenitor star of gamma-ray burst GRB 021004 and the properties of its host galaxy with high-resolution echelle and near-infrared spectroscopy. Methods. Observations were taken by the 8.2 m Very Large Telescope with the Ultraviolet and Visual Echelle spectrograph (UVES) and the Infrared Spectrometer And Array Camera (ISAAC) between 10 and 14 h after the onset of the event.Results. We report the first detection of emission lines from a GRB host galaxy in the near-infrared, detecting Hα and the [O III] doublet. These allow us to independently measure the systemic redshift (z = 2.3304 ± 0.0005), which is not contaminated by absorption as the Lyα line is, and infer the host galaxy properties. From the visual echelle spectroscopy, we find several absorptionline groups spanning a range of about 3000 km s −1 in velocity relative to the redshift of the host galaxy. The absorption profiles are very complex with both velocity-broadened components extending over several 100 km s −1 and narrow lines with velocity widths of only ∼20 km s −1 . By analogy with QSO absorption line studies, the relative velocities, widths, and degrees of ionization of the lines ("line-locking", "ionization-velocity correlation") show that the progenitor had both an extremely strong radiation field and several distinct mass-loss phases (winds). Conclusions. These results are consistent with GRB progenitors being massive stars, such as luminous blue variables (LBVs) or Wolf-Rayet stars, providing a detailed picture of the spatial and velocity structure of the GRB progenitor star at the time of explosion. The host galaxy is a prolific star-forming galaxy with a SFR of ∼40 M yr −1 .