Bechoros 11a rules that one may redeem someone else's firstborn donkey on their behalf. The Gemara asks: If someone were to do this, who gets to keep the donkey?
Now, there's a dispute between R' Yehudah and R' Shimon regarding whether one may derive benefit from a firstborn donkey before it's been redeemed. According to R' Shimon, who says that one may derive benefit from it, the question is invalid: the donkey is the property of its owner, and redeeming it onto a sheep does not change this. The question is according to R' Yehudah, that one may not benefit from a firstborn donkey before it's been redeemed:
להקדש מדמי ליה ורחמנא אמר ונתן הכסף וקם לו או דלמא כיון דקני להו בביני וביני לא דמי להקדש
Is it compared to Hekdesh, and the Torah says, "He will give the money and it will be to him," [i.e. to the one who redeems it]? Or maybe since he [the original owner] owns the difference [as one can redeem onto a sheep of lesser value than the donkey], it's not similar to Hekdesh?
The Gemara ultimately concludes like the latter approach; however, I'd like to try to understand the former approach.
If Shimon is able to redeem and take Reuven's donkey, effectively he's forcing Reuven into a transaction which he may not have wanted. This would seem to be a clear violation of Lo Sachmod (cf. Bava Metzi'a 5b). How could the Gemara possibly entertain that the one who redeems the donkey gets to keep it, if he's violating a Torah prohibition in the process?