Direct measurement of neural activity in freely moving animals is essential for understanding how the brain controls and represents behaviors. Genetically encoded calcium indicators report neural activity as changes in fluorescence intensity, but brain motion confounds quantitative measurement of fluorescence. Translation, rotation, and deformation of the brain and the movements of intervening scattering or auto-fluorescent tissue all alter the amount of fluorescent light captured by a microscope. Compared to single-photon approaches, two photon microscopy is less sensitive to scattering and off-target fluorescence, but more sensitive to motion, and two photon imaging has always required anchoring the microscope to the brain. We developed a closed-loop resonant axial-scanning high-speed two photon (CRASH2p) microscope for real-time 3D motion correction in unrestrained animals, without implantation of reference markers. We complemented CRASH2p with a novel scanning strategy and a multi-stage registration pipeline. We performed volumetric ratiometrically corrected functional imaging in the CNS of freely moving Drosophila larvae and discovered previously unknown neural correlates of behavior.