We construct an explicit minimal model for an algebra over the cobar-construction of a differential graded operad. The structure maps of this minimal model are expressed in terms of sums over decorated trees. We introduce the appropriate notion of a homotopy equivalence of operadic algebras and show that our minimal model is homotopy equivalent to the original algebra. All this generalizes and gives a conceptual explanation of well-known results for A∞-algebras. Furthermore, we show that these results carry over to the case of algebras over modular operads; the sums over trees get replaced by sums over general Feynman graphs. As a by-product of our work we prove gauge-independence of Kontsevich's ‘dual construction’ producing graph cohomology classes from contractible differential graded Frobenius algebras.