Let overline{M}_{g,n} be the moduli space of stable algebraic curves of genus g with n marked points. With the operations which relate the different moduli spaces identifying marked points, the family (overline{M}_{g,n})_{g,n} is a modular operad of projective smooth Deligne-Mumford stacks, overline{M}. In this paper we prove that the modular operad of singular chains C_*(overline{M};Q) is formal; so it is weakly equivalent to the modular operad of its homology H_*(overline{M};Q). As a consequence, the "up to homotopy" algebras of these two operads are the same. To obtain this result we prove a formality theorem for operads analogous to Deligne-Griffiths-Morgan-Sullivan formality theorem, the existence of minimal models of modular operads, and a characterization of formality for operads which shows that formality is independent of the ground field