The effect you describe is called a negative after image. It can be explained by adaptation effects of the photoreceptors in the eye.
source: Dresden University
Steadily fixate on the black lightbulb for thirty seconds or more and then immediately turn your gaze to the white region on the right. The illusionary glowing white bulb you perceive after fixating on the black picture is called an afterimage.
When the eyes and hence the photosensitive layer therein, the retina, is focused on an image, light-sensitive photoreceptors in the retina respond to the incoming light. In turn, secondary neurons carry the information to the brain via the optic nerve. When photoreceptors are continuously activated, they become desensitized (or fatigued); it is said they adapt to the stimulus (Kalloniatis & Luu, 2012).
Adaptation occurs at multiple stages in the visual system. One of the important processes behind visual adaptation in this example is photopigment "bleaching" due to constant stimulation. The desensitization is strongest for cells viewing bright stimuli and is caused by the photopigments in the photoreceptors being all used up after continuous activation.
Back to the light bulb illusion above: photobleaching is strongest for cells that were activated by the bright parts of the image, but weaker for cells viewing the darkest part of the figure. Then, when you avert the focus onto the white screen, the least depleted cells (the ones not adapted) will respond strongly, while the adapted cells will be unresponsive. hence, the retina focused on the black light bulb will respond vigurously, while the more peripheral parts of the retina that received the white part of the image surrounding the bulb will be silent. Hence, a glowing light bulb will be perceived in the after image.
This particular phenomenon is called a negative afterimage, in which bright areas of the figure turn dark and vice versa. This will happen when looking for prolonged periods of time to a certain image (this answer), or when looking directly in very bright sources like the sun (as in the question). Positive afterimages also exist, but do not apply to this specific question.
- Kalloniatis & Luu, Light and Dark Adaptation. In: Webvision (2012). Utah University