Calcium imaging with cellular resolution typically requires an animal to be tethered under a microscope, which substantially restricts the range of behaviors that can be studied. To expand the behavioral repertoire amenable to imaging, we have developed a tracking microscope that enables whole-brain calcium imaging with cellular resolution in freely swimming larval zebrafish. This microscope uses infrared imaging to track a target animal in a behavior arena. On the basis of the predicted trajectory of the animal, we applied optimal control theory to a motorized stage system to cancel brain motion in three dimensions. We combined this motion-cancellation system with differential illumination focal filtering, a variant of HiLo microscopy, which enabled us to image the brain of a freely swimming larval zebrafish for more than an hour. This work expands the repertoire of natural behaviors that can be studied with cellular-resolution calcium imaging to potentially include spatial navigation, social behavior, feeding and reward.