These sensations of light (for they are not actually light) themselves are called phosphenes.
They occur when the retina or visual cortex is stimulated by something other than light waves, which are the normal stimuli. Mechanical pressure like rubbing the eyes when they are closed, or electrical or magnetic stimulation can cause the effect of light. Pressure phosphenes can persist briefly after the eye is reopened.
Phosphenes can also accompany a blow to the head, or standing too suddenly...the "seeing stars" phenomenon.
They may also be the product of retinal or neural disease.
[image from sharonthebaron's moblog]