A known fundamental issue with atomic force microscopy (AFM) is that drift occurs during an AFM measurement, distorting the AFM image. In this study, a method for correcting this nonlinear drift in two dimensions (the vertical axis and one of the two horizontal axes) is proposed and demonstrated. A normal AFM measurement is accomplished with many fast-scan profiles, using the raster scan method. In the proposed drift-correction method, the first-scanned profile is set as the reference profile, and the scan at the first-scanned location is inserted periodically during the normal profile scans. The normal scanned profiles are used to construct a normal AFM image, which is distorted by the drift. The time-dependent drift distance can be estimated by a series of the scanned reference profiles, and the distorted AFM image is corrected using this estimated distance. It is shown, experimentally, that the drift correction in two dimensions has both high resolution and repeatability at the sub-nanometer scale.