King John V ordered it to fulfil a vow made in 1711: should Queen Maria Anna of Austria bear him children, a convent would follow. The first stone was laid on 17 November 1717, and the basilica was consecrated on 22 October 1730, the king's 41st birthday. UNESCO inscribed the palace, basilica, convent, Cerco Garden and Tapada as a World Heritage Site in 2019.

What began as a modest friary for 13 Capuchin friars grew into a vast Baroque complex once gold and diamonds from colonial Brazil began reaching Lisbon. The final design is credited to the German-born João Frederico Ludovice (Johann Friedrich Ludwig), drawing on projects by Filippo Juvarra, with the kingdom's chief engineer Custódio Vieira directing the works. Thousands of labourers from across the country worked on the site, and José Saramago's novel Baltasar and Blimunda (Memorial do Convento) follows one of them. The building has about 1,200 rooms and 156 stairways. Work went on until 1755, when the workforce was needed in Lisbon after the earthquake.

The white marble basilica sits at the centre of the façade, flanked by the two wings of the palace. John V wanted it to rival Rome, and its twin towers, 68 m high, and its dome take their cue from Borromini's Sant'Agnese in Agone, while the central balcony echoes that of St. Peter's. Inside, pink and white marble patterns run across floor and ceiling, 58 marble statues by Roman sculptors line the aisles, and a great jasper crucifix carved by the School of Mafra, with two angels kneeling beside it, hangs above the high altar. The church also holds six organs, built between 1792 and 1807 by António Xavier Machado e Cerveira and Joaquim Peres Fontanes and designed to be played together, and the towers carry two carillons cast in Antwerp and Liége, with clocks and automatic music machines of the 18th century.

The royal apartments lie on the second floor, with the king's rooms at one end and the queen's at the other; tradition says a trumpet announced the king whenever he set off down the corridor towards her. Much of the furniture left for Brazil with the royal family in 1807, so most rooms were later redecorated in the original style. Among the rooms shown are the Benediction Gallery, from which the royal family followed Mass through a window into the basilica, the Throne Room with murals by Cirilo Wolkmar Machado, Vieira Lusitano and others, and the Hunting Trophy Room, furnished with antlers and deerskin. The last king, Manuel II, left from Mafra for Ericeira and exile on 5 October 1910.

The palace's most celebrated room is the Rococo library at the back of the second floor, 88 m long, with a floor of pink, grey and white marble and two tiers of wooden shelves divided by a balcony. It keeps more than 30,000 volumes, the learning of an 18th-century royal court, among them incunabula. A colony of bats lives in the room and keeps insects from harming the books. Behind the church and palace, long corridors of cells once housed the friars of the Arrábida Franciscans.