Madara had activated Izanagi in his right eye. Using this forbidden jutsu, he came back to life after Hashirama killed him.
Because he used Izanagi, the light/sight of the right eye was removed. Because that's the price you have to pay when you use Izanagi or Izanami.
Madara had bit off a piece of Hashirama's flesh during the battle. After he came back alive, he went into hiding, planting a clone in his tomb. Then he surgically attached the flesh onto his body and waited.
Near the end of his natural life, he awoke the Rinnegan in both eyes. This awakening restored the sight in his right eye, a unique property of the Rinnegan.
Since he was nearing death and was unable to complete his plans, he transplanted his eyes to a Senju descendant, Nagato. The technique he used is not mentioned anywhere, but it would be a piece of cake for him, because of his intelligence and the help from Zetsu.