I know Muslims were allowed to keep captives of war, and although this is hypothetical, were they allowed to buy slaves from other people (i.e. non-Muslims whose slaves were from other sources) or was war the only source of slaves for them?
Basically the answer yes of course they were allowed and encouraged to buy slaves and set them free especially if these slaves were believers and also as a way to repent from sins or as a kafara (expiation). Therefore freeing slaves (either the own ones or by buying those of others) was considered a good deed or one of the highest levels of expiation and a way to spend the money for zakat for (see for example 2:177, 4:92, 5:89, 9:60, 58:3, 90:13). Note that the fact that Islam allows making contracts with slaves is also an indictaion that slaves would be able to buy themselves free.
I will try to elaborate later but would recommend you to take a look at my answer here Are Muslim men allowed to take "sex slaves?"