Under the Abbasids, enslaved people could gain their freedom if:
Enslaved people under Islam have certain rights and if the master does not grant these rights, the enslaved person can sue for their freedom.
Some enslaved people were also allowed to buy their freedom from their masters after some time and some were freed when their masters died.
hope this helps!