Retina stimulation. Hitting the eye causes a pressure wave that hits the film layer (retina) in the back of the eye. This stimulates the receptors in the retina to fire, creating the lights that you see.
Nerve stimulation. Nerves can be stimulated by various things. When you hit your "funny bone, " you are stimulating your ulnar nerve in your elbow. When you poke your eye, you are stimulating your optic nerve in a similar fashion, causing the sparkly lights (and a bit of pain).
Mechanical stimulati. The retinal photoreceptors are mechanically stimulated when an eye gets poked. The brain always interprets that stimulation as a light.