Mary, Jesus’ mother, was still living when Matthew wrote his account and would have gotten her firsthand eyewitness testimony. Luke says he interviewed eyewitnesses in his account which may have included Mary. There may have been other eyewitness who saw the birth of Jesus that we are unaware of.
Many historical accounts of famous people were written by people who weren’t physically present but interviewed people who were.
And this was all written down at least 70 years after the crucifixion.So that would be about a hundred years after his birth. I am sure they got all the details exactly as it happened.