You include this factor $\sqrt{-g}$ in order to make the action invariant under diffeomorphisms. If you did not include it, it would be the integral of a scalar tensor density of weight different from zero (I think +1 or -1, I don't really remember how the convention works), meaning that as you said the measure would change of a determinant factor.
By including $\sqrt{-g}$ you remove this change, since $$g=det(g_{\mu\nu})\rightarrow det\left(g_{\tau\lambda}\frac{\partial x^{\tau}}{\partial x'^{\mu}}\frac{\partial x^{\lambda}}{\partial x'^{\nu}}\right)=g\,\cdot\,(|J|^{-1})^2=g'$$
and
$$d^4x\rightarrow det\left(\frac{\partial x'}{\partial x}\right)d^4x=|J|\,d^4x=d^4x'$$
So in conclusion
$$d^4x\sqrt{-g}\quad\rightarrow\quad d^4x\sqrt{-g}|J|\cdot|J|^{-1}=d^4x\sqrt{-g}$$
and the action is invariant, provided the Lagrangian density is a scalar.