The self-couplings of the electroweak gauge bosons are probed at hadron colliders through the production of a massive gauge boson and a photon. To extend the theoretical description of this type of final states towards next-to-next-to-leading order (NNLO) in QCD, we derive the two-loop QCD corrections to the helicity amplitudes describing the production of a massive gauge boson in association with a real photon. Our results are obtained by applying projectors to the general parton-level tensor structure. The leptonic decay of the vector boson is included, thus allowing for a fully exclusive description of the final state. The infrared poles of the amplitudes are described by an infrared factorization formula. We provide an analytic expression for the finite remainder of the amplitude in terms of one- and two-dimensional harmonic polylogarithms. The amplitudes are expressed in the physical kinematics relevant to gauge-boson-plus-photon production at hadron colliders. As a by-product, we also derive the two-loop QCD amplitudes for vector-boson-plus-jet production at hadron colliders.