There are two facets to the answer, one regarding the nature of the sin offering itself, and one regarding Mary’s motivations for making the offering.
In summary, in Israel, so-called “sin” offerings were offered for transgressions agains the ritual law, not so much for offenses against the moral law.
Moreover, just as Jesus submitted himself to the baptism of John, even though he did not need to repent of any sins, Mary wished to fulfill the requirements of the Jewish law out of loving obedience to God.
There is, thereore, no contradiction between Mary’s sinlessness (in the moral sense) and her offering a sacrifice to remove the merely ritual impurity associated with childbirth.
Regarding the sin offering itself
The sin offering for women after childbirth, as the O.P. points out, is spelled out in chapter 12 of Leviticus.
In ancient Israel, women were considered ritually unclean for a few weeks after the birth of her child. (It varied according to the sex of the child; a total of 40 days for a boy, and 80 days for a girl. See vv. 2-6.) That essentially meant that they were unable to partake of the liturgical celebrations until their uncleanliness was over, at which time they were to make a sin offering, or either a lamb or a pair of pigeous or turtledoves (vv. 6-8).
But it is important to note that ritual uncleanliness had nothing to do with moral uncleanliness. Leviticus chapter 4 introduces the concept of sin offerings in this way:
And the Lord spoke to Moses, saying, “Speak to the people of Israel, saying, If anyone sins unintentionally in any of the Lord’s commandments about things not to be done, and does any one of them, if it is the anointed priest who sins … [and it goes on to spell out what each group should do] (Lev. 4:1-3).
Sin offering could only be offered for unintentional transgressions and, in general for the removal of ritual uncleanliness. There was, in fact, no provision in the Law for the forgiveness of moral offenses—and this lack was one of the constant sufferings of the People of Israel.
(The problem of the forgiveness of sin, in the proper sense of the term, is the topic of the second half of 2 Samuel, for example: King David commits some grave moral offenses—his adultery with Bathsheeba, the murder of Uriah, and his taking of the census against God’s orders—and yet the Law offers no provisions for reconciliation with God. Although God makes his forgiveness known through the prophets, he is extremely stern by our standards.)
In the words of the Letter to the Hebrews, “it is impossible for the blood of bulls and goats to take away sins [in this case, moral offenses]” (Heb. 10:4). For that, says Hebrews, the Blood of Jesus Christ is necessary.
Hence, even though Mary herself was completely endowed with grace (kecharitomene, Lk 1:28) at the birth of Jesus, and even though her son was, evidently, perfectly pure, she still incurred ritual uncleanliness under the Law of Moses.
It should be observed that the Law did not make any exceptions. The moral character of the woman was never considered; all women had to make the sin offering after childbirth.
Why Mary chose to fulfull the Law
Was Mary strictly obliged to make the offering? No, because she was already full of grace and, in fact, already redeemed. Mary, however, fulfilled the prescriptions of the Law out of loving obedience to God. (It probably never occurred to her to ask whether she was “exempt” from the Law or not.)
Jesus did something similar when he received the baptism of John. Jesus was also sinless and (unlike Mary) incapable of sinning; and yet he received the baptism of repentence, because it was “fitting … to fulfill all righteousness” (Mt. 3:15).