Local anesthetics effectively suppress pain sensation, but most of these compounds act nonselectively, inhibiting activity of all neurons. Moreover, their actions abate slowly, preventing precise spatial and temporal control of nociception. We developed a photoisomerizable molecule, quaternary ammonium–azobenzene–quaternary ammonium (QAQ), that enables rapid and selective optical control of nociception. QAQ is membrane-impermeant and has no effect on most cells, but it infiltrates pain-sensing neurons through endogenous ion channels that are activated by noxious stimuli, primarily TRTRPV1. After QAQ accumulates intracellularly, it blocks voltage-gated ion channels in the trans form but not the cis form. QAQ enables reversible optical silencing of mouse nociceptive neuron firing without exogenous gene expression and can serve as a light-sensitive analgesic in rats in vivo. Because intracellular QAQ accumulation is a consequence of nociceptive ion-channel activity, QAQ-mediated photosensitization is a platform for understanding signaling mechanisms in acute and chronic pain.