Active galactic nuclei in the mid-IR. Evolution and contribution to the cosmic infrared background