Context. The stars of the middle main sequence have relatively quiescent outer layers, and unusual chemical abundance patterns may develop in their atmospheres. The presence of chemical peculiarities reveal the action of such subsurface phenomena as gravitational settling and radiatively driven levitation of trace elements, and their competition with mixing processes such as turbulent diffusion. At present, little is known about the time evolution of these anomalous abundances, nor about the role that diffusion may play in maintaining them, during the main sequence lifetime of such a star. Aims. We want to establish whether abundance peculiarities change as stars evolve on the main sequence, and provide observational constraints to diffusion theory. Methods. We have performed spectral analysis of 15 magnetic Bp stars that are members of open clusters (and thus have well-known ages), with masses between about 3 and 4 M . For each star, we measured the abundances of He, O, Mg, Si, Ti, Cr, Fe, Pr and Nd. Results. We have discovered the systematic time evolution of trace elements through the main-sequence lifetime of magnetic chemically peculiar stars as their atmospheres cool and evolve towards lower gravity. During the main sequence lifetime, we observe clear and systematic variations in the atmospheric abundances of He, Ti, Cr, Fe, Pr and Nd. For all these elements, except He, the atmospheric abundances decrease with age. The abundances of Fe-peak elements converge towards solar values, while the rare-earth elements converge towards values at least 100 times more abundant than in the Sun. Helium is always underabundant compared to the Sun, evolving from about 1% up to 10% of the solar He abundance. We have attempted to interpret the observed abundance variations in the context of radiatively driven diffusion theory, which appears to provide a framework to understand some, but not all, of the anomalous abundance levels and variations that we observe.