It is reported from Imam al-Sha'bi (may Allah have mercy on him) regarding a man who lets his animal go, and then another man takes it and restores it: al-Sha'bi (may Allah have mercy on him) said: "This has already been ruled upon: if he let it go in a place of pasture, water and safety, its original owner has more right to it; and if he let it go in a desert and a place of danger, the one who took it has more right to it."
Hadith Referenceالسنن الكبرى للبيهقي / كتاب اللقطة / 12116