Basically the doctrine of the Immaculate Conception Proclaimed infallibly by Pope Pius IX in 1854 sums up the theological reasoning.
"in the first instance of her conception, by a singular privilege and grace granted by God, in view of the merits of Jesus Christ, the Saviour of the human race, was preserved exempt from all stain of original sin."
Not only was it necessary for Mary to be a virgin, it was necessary for her to be free from the stain of original sin. If she was not a virgin, she would have sinned in some way on account of the fact that she was espoused to Joseph, but had not yet known him. She didn't 'know' him for a reason. It's reasonable to suppose that they were in some sort of period of betrothal before an official marriage, when sexual relations would have been an occasion of sin.
This is why we know she was sinless:
- She is often referred to as the new Eve by the early church fathers. Eve was made free from original sin and it was through her and Adam that sin entered the world. Mary likewise would have to be free from original sin to be the Mother of God in order for redemption to enter the world.
- She is a living tabernacle, the holy of holies, when Jesus was within her. Nothing impure could contain Him and live.
- She is told she is 'full of grace' by an angelic witness who would not lie or mince words before the Incarnation of Jesus.
More info