The fundamental properties of light derive from its constituent particles—massless quanta (photons) that do not interact with one another. However, it has long been known that the realization of coherent interactions between individual photons, akin to those associated with conventional massive particles, could enable a wide variety of novel scientific and engineering applications. Here we demonstrate a quantum nonlinear medium inside which individual photons travel as massive particles with strong mutual attraction, such that the propagation of photon pairs is dominated by a two-photon bound state. We achieve this through dispersive coupling of light to strongly interacting atoms in highly excited Rydberg states. We measure the dynamical evolution of the two-photon wavefunction using time-resolved quantum state tomography, and demonstrate a conditional phase shift exceeding one radian, resulting in polarization-entangled photon pairs. Particular applications of this technique include all-optical switching, deterministic photonic quantum logic and the generation of strongly correlated states of light.