The emission mechanism of the gamma-ray bursts ( GRBs ) is still a matter of debates . The standard synchrotron energy spectrum of cooling electrons F _ { E } \propto E ^ { -1 / 2 } is much too soft to account for the majority of the observed spectral slopes . An alternative in the form of quasi-thermal Comptonization in a high compactness source has difficulties in reproducing the peak of the observed photon distribution below a few hundred keV . We show here that for typical parameters expected in the GRB ejecta the observed spectra in the 20–1000 keV BATSE energy range can be produced by inverse Compton scattering of the synchrotron radiation in a partially self-absorbed regime . If the particles are continuously accelerated/heated over the life-time of a source rather than being instantly injected , a prominent peak develops in their distribution at a Lorentz factor \gamma \sim 30 - 100 , where synchrotron and inverse-Compton losses are balanced by acceleration and heating due to synchrotron self-absorption . The synchrotron peak should be observed at 10–100 eV , while the self-absorbed low-energy tail with F _ { E } \propto E ^ { 2 } can produce the prompt optical emission ( like in the case of GRB 990123 ) . The first Compton scattering radiation by nearly monoenergetic electrons can then be as hard as F _ { E } \propto E ^ { 1 } reproducing the hardness of most of the observed GRB spectra . The second Compton peak should be observed in the high energy gamma-ray band , possibly being responsible for the emission detected by EGRET in GRB 941017 . A significant electron-positron pair production reduces the available energy per particle , moving the spectral peaks to lower energies as the burst progresses . The regime is very robust , operates in a broad range of parameter space and can explain most of the observed GRB spectra and their temporal evolution .