Our Gemara is discussing the case of an eved kena’ani – a non-Jewish slave – and Rav Yehuda quotes Shmuel as teaching that releasing a non-Jewish slave is forbidden because it involves violating a positive commandment: “Le-olam ba-hem ta’avodu – of them may you take your bondmen forever,” requiring that one subjugate slaves their entire lives (see Vayikra 25:46). In fact, as the Gemara points out, Rabbi Yishmael believes that this passage is reshut – it gives permission to behave in this way, but does not require it. Rabbi Eliezer, however, views it as a commandment. The Gemara follows this up with a story describing how Rabbi Eliezer once found that the synagogue lacked a minyan of ten people and he freed his slave so that there would be the required ten, explaining that for the purpose of a mitzva it would be permissible.
Some of the rishonim explain the possible prohibition against freeing a non-Jewish slave as stemming from the idea of lo tehanem – according to which giving a gift to a non-Jew with no expectation of reciprocity is forbidden (see Devarim 7:2). If this is the source of the prohibition then we can conclude that as long as the slave pays for his release, or if it is in the interest of the owner to free the slave, then the prohibition is no longer in place.
It should be noted that every eved kena’ani is obligated in some level of mitzvot, a situation that leads the Rashba to conclude that he cannot be considered a non-Jew, and lo tehanem would not apply. According to this approach, we have a unique command of le-olam ba-hem ta’avodu, which would fall away should there be any good reason to set the slave free. So if the slave performed a particularly meritorious act towards his owner or his family, the owner can choose to set him free, viewing it as a type of payment in kind for his service.