A photon is spontaneously emitted or absorbed by an atom in that a bound electron of its shell changes the distance to the atomic nucleus. Photons also emerge in the irradiation of particles and antiparticles, like electron and positron, at their encounter, and they disappear in the reverse process of pair formation.
Free charged particles emit photons spontaneously if they are slowed down (Bremsstrahlung) or accelerated - as via alternating electromagnetic fields in a hollow resonator, or cavity (figure: sketch of the process in an accelerating cavity of the TESLA project).