A:
The first sale was valid. However,
the second sale, after the shop owner had agreed with you was invalid, because he sold something he did not own. As for your giving up the ownership of the shop to someone else and taking money in return, there is nothing wrong with this.
May Allah grant us success. May peace and blessings be upon our Prophet Muhammad, his family, and Companions.