The fabrication of hierarchical and porous structures holds significant promise in biomedical science, aerospace, and related fields. Despite advancements in 3D printing technology that have enhanced the capability of complex porous structures, its application in metal fabrication remains challenging. In this paper, we introduce a method involving a 3D printed sacrificial template, created by the material extrusion process, for magnesium (Mg) infiltration. This method facilitates the production of porous Mg scaffolds, with the templates subsequently being water-leached. The morphology, shrinkage rate, compressive strength, and types of lattices of templates were observed and assessed to determine their viability. Further, comprehensive compression tests and cell adhesion experiments were conducted on the Mg scaffolds to evaluate their performance. The results suggest that the templates must possess adequate strength and continuous flow channels for successful Mg infiltration. The scaffolds experienced an average of about 30 % shrinkage from the initial design to the final structure. The porous Mg scaffolds exhibit enhanced compressive strength (47.48 ± 0.84 MPa) at lower porosity (0.52) and demonstrate satisfactory biocompatibility. This research highlights the efficacy and low cost of the template replication method in generating mechanically superior porous Mg scaffolds with customized pore sizes and structures. In future research, this method also holds promise for applications involving newly developed alloys.