Ballistic quantum wires are exposed to longitudinal profiles of perpendicular magnetic fields composed of a spike (magnetic barrier) and a homogeneous part. An asymmetric magnetoconductance peak as a function of the homogeneous magnetic field is found, comprising quantized conductance steps in the interval where the homogeneous magnetic field and the magnetic barrier have identical polarities, and a characteristic shoulder with several resonances in the interval of opposite polarities. The observations are interpreted in terms of inhomogeneous diamagnetic shifts of the quantum wire modes leading to magnetic confinement.