The question you ask can be applied to any part of the bible. Some are even much older than the new testament.
My belief, Mary sang the Magnificat exactly like it is written.
[Here comes a detail, who gave the exact title:“Magnificat”? That part might be a posterior addition, by an author inspired by the Holy Spirit, or a simple title approved by the church.]
Now, how was the Magnificat then repeated until it found it’s way into the Gospel? I believe Mary remembered it and repeated it throughout her life. It can be singed by any of us praising God. Certainly the Holy Spirit guided all those involved so that the Magnificat entered the bible. Exactly how is lost in time, and perhaps the exact circumstances are not the most important. Like we don’t know the exact height of saint Mark, or the eye color of saint James…What we do know is that what is contained in Sacred Scripture is what matters.
Also, Mariology and bible study probably go a long way in explaining as best as possible the circumstances in which Mary lived and in which the bible was written.