A novel method using mesoporous g-C3N4 and β-cyclodextrin nanocomposite (mpg-C3N4/β-CD) for the electrochemical detection of 2,4,6-trinitrotoluene (TNT) is presented. The unique structure of the mesoporous g-C3N4/β-CD nanocomposite facilitates both mpg-C3N4 electrocatalysis and β-CD inclusion-complexation of the analytes. When compared to GCE or mpg-C3N4 modified GCE, the mpg-C3N4/β-CD modified glassy carbon electrode exhibits superior performance in the detection of TNT. Hence, we used mpg-C3N4/β-CD modified GCE for the development of TNT detection method using linear sweep voltammetry at −0.45 V reduction potential with 100 s accumulation time. The TNT calibration curve is linear within the 1–100 μM concentration range and the corresponding sensitivity and LOD values are 0.2 μA/μM and 68 ppb respectively. When p-nitrophenol and o-nitrophenol introduced into the matrix, the mpg-C3N4/β-CD modified GCE sensor showed high selectivity and sensitivity to the nitroaromatic compounds. Optimized mpg-C3N4/β-CD modified GCE sensor was used for the detection of TNT in a natural lake water sample with an 96.8% peak recovery. The results shown in work illustrate the potential of using g-C3N4/β-CD modified GCE sensors in monitoring TNT and other nitroaromatic compounds in environmental analysis.