The scene is based not on any incident in the Bible itself, but on a body of tales or legends that had grown up in the early Middle Ages around the Bible story of the Holy Family fleeing into Egypt for refuge on being warned that Herod the Great was seeking to kill the Christ Child. According to the legend, Joseph and Mary stopped on the flight in a grove of trees; the Holy Child ordered the trees to bend down so that Joseph could take fruit from them, and then ordered a spring of water to gush forth from the roots so that his parents could quench their thirst. The basic story took on many extra details during the centuries. Caravaggio shows Mary asleep with the infant Jesus, while Joseph holds a manuscript for an angel who is playing a hymn to Mary on the violin. The ass, a traditional ingredient of the legend and a foretelling of the ass that would one day bear Christ into Jerusalem, watches.
The date of the painting is disputed, and the dispute illustrates the difficulties of Caravaggio's oeuvre. According to Caravaggio's contemporary Giulio Mancini, this painting and the Penitent Magdalene, together with an unidentified Saint John the Evangelist, was done while Caravaggio was staying with monsignor Fantino Petrignani, shortly after leaving the workshop of Giuseppe Cesari. This probably happened in January 1594. However, there are problems with accepting Mancini's statement. To begin, none of these three works were listed in Petragnani's inventory of 1600, although it's possible that they could have been painted for another patron. More seriously, the painting has an obvious and direct compositional source in Annibale Carracci's Judgement of Hercules, which was completed early in 1596 and widely admired. The pose of Caravaggio's angel, for example, is closely based on that of Carracci's figure of Vice. While John Gash (2003) accepts Mancini's testimony, others, including Robb and Langdon (both 1998), have raised the possibility that it was painted for Cardinal Francesco Maria Del Monte, who made Caravaggio in effect his household artist from about 1595 or 1596. The sophisticated treatment is appropriate for the cardinal's intellectual tastes and interests (the music held by Joseph is a motet by the Flemish composer Noel Bauldeweyn, with a text from the Song of Songs dedicated to the Madonna, Quam pulchra es, "How beautiful you are"), and it's unlikely that the artist would embark on a work like this other than as a direct commission.
This was the first large-scale work done by Caravaggio, and compositionally both more ambitious and more successful than The Musicians, of about 1595. It's also one of the very rare landscapes from this artist who seems always to have been painting in a prison cell, a room at a tavern, or at night - one critic has joked that all the sky in all Caravaggio's 80-odd works would add up to a few square centimeters of paint. The painting was apparently sold to the Pamphilj by the early 1600s. Caravaggio's Lombard formation and Venetian heritage are evident in the treatment of the landscape and in the luminous tonalities. Like most depictions of the flight to Egypt this is a peaceful moment, one in which the scenery is to be enjoyed, more gardenscape than landscape. The luminous figure of the adolescent angel, at once serene and sensuous, holds the centre of the group. The mother and child grouping, one of many that Caravaggio would paint, is comparable in its delicacy and realism to the best that the thousands in the canon can offer.
One of the great pastimes of Caravaggio scholars is identifying his models. Much progress has been made, but the following should be regarded as tentative only, as Caravaggio left few clues. Mary appears to be the same girl who appears as Mary Magdalen in the Penitent Magdalene of about 1597, also in the Doria-Pamphilj Gallery. The aged Joseph appears similar to the elderly saints in The Inspiration of Saint Matthew of 1602 and, less clearly, Saint Jerome in Meditation of about 1605. Some critics have identified the boy-angel with the ingenuous victim of cheats on the left of Cardsharps, while others have seen a similarity with the profile of the boy cheating him instead.