Context. In this work a mass–effective temperature–surface gravity relation (MTGR) is developed for main-sequence stars in the range 6400 K ≤ Teff ≤ 20 000 K with log g ≳ 3.44. The MTGR allows the simple estimation of the masses of stars from their effective temperatures and surface gravities. It can be used for solar metallicity and can be rescaled for any metallicity within −1.0 ≤ [Fe/H] ≤ 0.7. The effect of α-enhanced compositions can also be considered with the help of correction terms. Aims. It is aimed to develop an MTGR that can estimate the masses of main-sequence stars from their atmospheric parameters. One advantage of an MTGR over the classical mass–luminosity relations is that its mass estimation is based on parameters that can be obtained by purely spectroscopic methods, and, therefore, the interstellar extinction or reddening do not have to be known. The use of surface gravity (g) also relates an MTGR with stellar evolution and provides a more reliable mass estimation. Methods. A synthetical MTGR is obtained from theoretical isochrones using a Levenberg-Marquardt χ2 minimization algorithm. The validity of the MTGR is then checked by testing over 278 binary components with precise absolute masses. Results. Very good agreement has been obtained between the absolute masses of 278 binary star components and their masses estimated from the MTGR. A mathematical expression is also given to calculate the propagated uncertainties of the MTGR masses. Conclusions. For the typical uncertainties in atmospheric parameters and metallicity (i.e., ±2.8% for Teff, ±0.1 dex for log g and ±0.15 dex for [Fe/H]) the typical uncertainties in the masses estimated from the MTGR mostly remain around 5–9%. The fact that this uncertainty level is only on average about three times as large as that of the absolute masses indicates that the MTGR is a very powerful tool for stellar mass estimation. A computer code, mtgr.pro, written in GDL or IDL is also provided for the relation.