How does a fetus retain a blood group different from its mother?
The maternal and fetal blood circulation systems are completely separate. The embryo's blood cells start developing at around week 5 gestational age (3 weeks after conception), the same way any other tissue is developed by the fetus itself. By around week 7 gestational age, a circulatory system has developed and the heart has started beating. 

All nutrition goes through the placenta, which keeps the two circulatory systems separate by the so-called placental blood barrier. 

Rhesus incompability is only a concern after the first birth (or miscarriage) because then the maternal and fetal blood had the opportunity to mix and lead to the mother developing antibodies. 

Also, as AMR pointed out in the comments, blood group incompatibility is mediated through IgG antibodies - those don't start to be produced at all until birth and at higher levels not until six months to a year after birth. See also At what age do babies begin to synthesize their own antibodies?