A composite foam, polyurethane–melamine formaldehyde (PU/MF) foam, was prepared through foaming PU resins in the three‐dimensional netlike skeleton of MF foam. The chemical structure, morphology, cell size and distribution, flame retardancy, thermal properties and mechanical properties of such composite foam were systematically investigated. It was found that the PU/MF foam possessed better fire retardancy than pristine PU foam and achieved self‐extinguishment. Moreover, no melt dripping occurred due to the contribution of the carbonized MF skeleton network. In order to further improve the flame retardancy of the composite foam, a small amount of a phosphorus flame retardant (ammonium polyphosphate) and a char‐forming agent (pentaerythritol) were incorporated into the foam, together with the nitrogen‐rich MF, thus constituting an intumescent flame‐retardant (IFR) system. Owing to the IFR system, the flame‐retardant PU/MF foam can generate a large bulk of expanded char acting as an efficient shielding layer to hold back the diffusion of heat and oxygen. As a result, the flame‐retardant PU/MF foam achieved a higher limiting oxygen index of 31.2% and exhibited immediate self‐extinguishment. It exhibited significantly reduced peak heat release rate and total heat release, as well as higher char residual ratio compared to PU foam. Furthermore, the composite foam also showed obviously improved mechanical performance in comparison with PU foam. Overall, the present investigation provided a new approach for fabricating a polymer composite foam with satisfactory flame retardancy and good comprehensive properties. © 2018 Society of Chemical Industry