This event in the early life of Christ is recounted in the Gospel of Saint Matthew (2: 13-15). Joseph was warned in a dream that Herod was intent on killing the child Jesus and so he took Mary and the baby to Egypt for safety.
When painted the story was often embellished following apocryphal texts. Often the Virgin is shown with the infant in her arms riding an ass. Sometimes guardian angels are present and occasionally the Holy Family are depicted crossing a river. In some accounts a meeting between Christ and his cousin John the Baptist also took place on this journey.
There is no biblical source for the Rest on the Flight into Egypt, a related and popular subject with painters.