Nagato getting rinnegan
Maybe the only explanation of how Nagato received the rinnegan from Obito was that Obito was saved and trained by Madara after he got squished under the rock.
Obito probably needed quite some time till he recovered and learned all these things during his training. That`s why I think that Madara fought the fourth hokage and not Obito. Madara didn`t have rinnegan then and said he awoke it short before he died.
He probably grew old or got killed by his student Obito who wanted to take over his identity because of his reputation. While he was still recovering, he took the rinnegan and knew he couldn`t handle it yet. He still needed time to get his `complete body`.
Then he found someone like Nagato who he could influence and control fully. He gave him the rinnegan and later he controlled him through the Akatsuki. Then, when Nagato died, he took back what was his.