Brief, intense bursts of γ-rays occur approximately daily from random directions in space, but their origin has remained unknown since their initial detection almost 25 years ago. Arguments based on their observed isotropy and apparent brightness distribution are not sufficient to constrain the location of the bursts to a local or cosmological origin. The recent detection of a counterpart to a γ-ray burst at other wavelengths, has therefore raised the hope that the sources of these energetic events might soon be revealed. Here we report spectroscopic observations of the possible optical counterpart, to the γ-ray burst GRB970508. The spectrum is mostly featureless, except for a few prominent absorption lines which we attribute to the presence of an absorption system along the line of sight at redshift z = 0.835. Coupled with the absence of Lyman-α forest features in the spectra, our results imply that the optical transient lies at 0.835 <= z ≲ 2.3. If the optical transient is indeed the counterpart of GRB970508, our results provide the first direct limits on the distance to a γ-ray burst, confirming that at least some of these events lie at cosmological distances, and are thus highly energetic.