We calculate the leading O(αs4) contributions to the invariant mass distribution of top-quark pairs produced at the Tevatron and LHC, in the limit where the invariant mass of the tt¯ pair approaches the partonic center-of-mass energy. Our results determine at NNLO in αs the coefficients of all singular plus distributions and scale-dependent logarithms in the differential partonic cross sections for qq¯,gg→tt¯+X. A numerical analysis showing the effects of the NNLO corrections on the central values and scale dependence of the invariant mass distribution is performed. The NNLO corrections are found to significantly enhance the cross section and reduce the perturbative uncertainties compared to the NLO calculation.