We develop a microscopic theory of spin relaxation of a two-dimensional electron gas in quantum wells with anisotropic electron scattering. Both precessional and collision-dominated regimes of spin dynamics are studied. It is shown that, in quantum wells with noncentrosymmetric scatterers, the in-plane and out-of-plane spin components are coupled: spin dephasing of carriers initially polarized along the quantum well normal leads to the emergence of an in-plane spin component even in the case of isotropic spin-orbit splitting. In the collision-dominated regime, the spin-relaxation-rate tensor is expressed in terms of the electric conductivity tensor. We also study the effect of an in-plane and out-of-plane external magnetic field on spin dephasing and show that the field dependence of electron spin can be very intricate.