We explore the hypothesis that the magnetic fields of neutron stars are of fossil origin. For parametrized models of the distribution of magnetic flux on the main sequence and of the birth spin period of the neutron stars, we calculate the expected properties of isolated radio pulsars in the Galaxy using as our starting point the initial mass function and star formation rate as a function of Galactocentric radius. We then use the 1374-MHz Parkes Multi-Beam Survey of isolated radio pulsars to constrain the parameters in our model and to deduce the required distribution of magnetic fields on the main sequence. We find agreement with observations for a model with a star formation rate that corresponds to a supernova rate of 2 per century in the Galaxy from stars with masses in the range 8-45 M-circle dot and predict 447 000 active pulsars in the Galaxy with luminosities greater than 0.19 mJy kpc(2). The progenitor OB stars have a field distribution which peaks at similar to 46 G with similar to 8 per cent of stars having fields in excess of 1000 G. The higher-field progenitors yield a population of 24 neutron stars with fields in excess of 10(14) G, periods ranging from 5 to 12 s, and ages of up to 100 000 yr, which we identify as the dominant component of the magnetars. We also predict that high-field neutron stars (log B > 13.5) originate preferentially from higher-mass progenitors and have a mean mass of 1.6 M-circle dot, which is significantly above the mean mass of 1.4 M-circle dot calculated for the overall population of radio pulsars.