Pupillary response is controlled by the autonomic nervous system.
In bright light or when looking at close objects, the parasympathetic nervous system will cause pupillary constriction by activating the sphincter pupillae muscle(s). The parasympathetic nerves that innervate the sphincter pupillae are the short ciliary nerves, which come from the ciliary ganglion in the orbit. Innervation of the ciliary ganglion is via parasympathetic pre-ganglionic fibers traveling with the occulomoter nerve from the edinger-westfall nucleus in the cranial midbrain.
In low light, when looking at distant objects, or when stress levels are high (fight or flight) the sympathetic nervous system will cause pupillary dilation by activating the iris dilator muscle(s). The sympathetic nerves that innervate these muscles come from the superior cervical ganglion.