Observations from BATSE show that the spectra of gamma-ray bursts are very wide, which cannot be described by a simple law. The distribution of the peak energy E_p [defined as the maximum of the E_gamma^2N(E_gamma) spectrum], and of the spectral indices below and above E_p, is wide. In particular, the slope below E_p lies mainly between 0.5 and 1, which is inconsistent with the 4/3 predicted by the simple synchrotron radiation spectrum. Here we discuss the emission model for gamma-ray bursts based on the shock model proposed by Meszaros & Rees. We find that the synchrotron radiation of relativistic electrons accelerated by a strong shock, together with the finite lifetime of synchrotron radiation and the non-uniform magnetic field distribution, can explain the gamma-ray burst spectra, and the spectral index below the peak energy E_p can be flattened by the non-uniform magnetic field distribution; this flattening is insensitive to the specific form of the magnetic field variance. Furthermore, the peak energy E_p~Lambda^4 is sensitively dependent on the bulk Lorentz factor.