Tradition tells us that Mary too was a descendant of David. According to Numbers 36:6-12, an only daughter had to marry within her own family so as to secure the right of inheritance. After St. Justin (Adv. Tryph. 100) and St. Ignatius (Letter to the Ephesians 18), the Fathers generally agree in maintaining Mary’s Davidic descent, whether they knew this from an oral tradition or inferred it from Scripture, e.g. Romans 1:3; II Timothy 2:8. St. John Damascene (De fid. Orth., IV, 14) states that Mary’s great-grandfather, Panther, was a brother of Mathat; her grandfather, Barpanther, was Heli’s cousin; and her father, Joachim, was a cousin of Joseph, Heli’s levirate son. Here Mathat has been substituted for Melchi, since the text used by St. John Damascene, Julius Africanus, St. Irenaeus, St. Ambrose, and St. Gregory of Nazianzus omitted the two generations separating Heli from Melchi. At any rate, tradition presents the Blessed Virgin as descending from David through Nathan.
{click here for source}