The photospheres of about 10–20 per cent of main‐sequence A‐ and B‐type stars exhibit a wide range of chemical peculiarities, often associated with the presence of a magnetic field. It is not exactly known at which stage of stellar evolution these chemical peculiarities develop. To investigate this issue, in this paper we study the photospheric compositions of a sample of Herbig Ae and Be stars, which are considered to be the pre‐main‐sequence progenitors of A and B stars. We have performed a detailed abundance analysis of 20 Herbig stars (three of which have confirmed magnetic fields), and one dusty young star. We have found that half the stars in our sample show λ Boötis (λ Boo) chemical peculiarities to varying degrees, only one star shows weak Ap/Bp peculiarities and all the remaining stars are chemically normal. The incidence of λ Boo chemical peculiarities we find in Herbig stars is much higher than what is seen on the main sequence. We argue that a selective accretion model for λ Boo star formation is a natural explanation for the remarkably large number of λ Boo stars in our sample. We also find that the magnetic Herbig stars do not exhibit a range of chemical compositions remarkably different from the normal stars: one magnetic star displays λ Boo chemical peculiarities (HD 101412), one displays weak Ap/Bp peculiarities (V380 Ori A) and one (HD 190073) is chemically normal. This is completely different from what is seen on the main sequence, where all magnetic A and cool B stars show Ap/Bp chemical peculiarities, and this is consistent with the idea that the magnetic field precedes the formation of the chemical peculiarities typical of Ap and Bp stars.