The movie is partially based on the apocryphal Gospel (or Protoevangelium) of James. It depicts Mary's miraculous conception and birth, as well as how she was offered in marriage to Joseph. The part where Herod the Great hears news from Bethlehem from his emissaries and describe how everything went motionless in that small town right after Baby Jesus was born is also contained in that apocryphal gospel.
In Matthew 1:18, we are not told about Mary's age, yet it is told that she was a virgin and was pledged to be married to Joseph. During this time in history, Jewish girls would have been betrothed to their husbands as early as the age of 12 years old. Scholars believe Mary would have been somewhere between 12 to 16 years old when she had Jesus. Other scholars say that ancient Jewish women would marry on average between their mid-teens and early twenties. It is therefore also possible that Mary was 18 when was betrothed to Joseph.