Having sex with slave girls, was a practice Islam inherited in Arabia from the past. As long as the problem of slavery was there despite the ruling that they should be set free at their will through the law of Mukatibat and no men or women should be made slaves from the captives of war as mentioned in the Quran, it was allowed to keep them the way they have been kept. Having sex with salve girls and the babies produced were legitimately ascribed to the owner of the slave girl.
This allowance is mentioned at several places in the Quran. one such places is Surah al-Mominoon, Chapter 23, Verses: 5-7
وَالَّذِينَ هُمْ لِفُرُوجِهِمْ حَافِظُونَ ﴿٥﴾ إِلَّا عَلَىٰ أَزْوَاجِهِمْ أَوْ مَا مَلَكَتْ أَيْمَانُهُمْ فَإِنَّهُمْ غَيْرُ مَلُومِينَ ﴿٦﴾ فَمَنِ ابْتَغَىٰ وَرَاءَ ذَٰلِكَ فَأُولَٰئِكَ هُمُ الْعَادُونَ
And they who guard their private parts (5) Except from their wives or those their right hands possess, for indeed, they will not be blamed – (6) But whoever seeks beyond that, then those are the transgressors –