We investigate the relation between the magnetocrystalline anisotropy energy (MAE) and the electronic structure for transition metal thin films and surfaces which can display enhanced orbital magnetic moments. When the spin-orbit interaction is treated in second order, the MAE is proportional to the expectation value of the orbital magnetic moment as given by Bruno's model. However, there are additional terms which are related to the spin-subband orbital moment and to the magnetic dipole operator due to the anisotropy of the field of the spin. The latter term accounts for the spin-flip excitations between the exchange split majority and minority spin bands. A conjecture is proposed which relates the MAE to the expectation values of the orbital moments and the magnetic dipole term. It is shown how the different terms can be obtained experimentally with (transverse) magnetic circular x-ray dichroism. The model explains the experimentally observed perpendicular magnetic anisotropy in Co and Fe based multilayers and thin films.