Having just finished Truth and Fiction in the Da Vinci Code by Bart Ehrman, I am curious about this question. I don't believe he answered it and my Professor claimed there is no biblical evidence behind this, and that it was added later.
It came from the conflation of Mary of Magdala with an anonymous “sinful woman,” mentioned in Luke 7:36-50, esp. 37-8 (English Standard Version):
And behold, a woman of the city, who was a sinner, when she learned that he was reclining at table in the Pharisee's house, brought an alabaster flask of ointment, and standing behind him at his feet, weeping, she began to wet his feet with her tears and wiped them with the hair of her head and kissed his feet and anointed them with the ointment.
This confusion was helped by a similar but different instance of anointment, reported by John, about another Mary, Mary of Bethany (John 12:3 and 11:1-2)
Mary therefore took a pound of expensive ointment made from pure nard, and anointed the feet of Jesus and wiped his feet with her hair.
Now a certain man was ill, Lazarus of Bethany, the village of Mary and her sister Martha. It was Mary who anointed the Lord with ointment and wiped his feet with her hair, whose brother Lazarus was ill.
The earliest (?) example of the conflation comes from a sermon by Gregory the Great (late 6th/early 7th c. pope), which explicitly articulates this conception and create a single figure from these three characters. As his nickname suggests, Gregory and his writings became quite authoritative in the West, and it is entirely possible that his bold hypothesis started the tradition from scratch.
/e people in /r/AcademicBiblical should be able to give a more detailed/affirmative answer.