"Origin of Pain" (ペイン誕生, Pein Tanjō) is episode 173 of the Naruto: Shippūden anime.
Nagato continues telling his story to Naruto and tells him how fate led the three Ame Orphans to the three legendary Sannin, and how Jiraiya offered to take care of them. After spotting Jiraiya's Konoha forehead protector, Nagato did not accept him, as it was Konoha shinobi who had murdered his mother and father. However, after spending time with Jiraiya, Nagato came to regard him as his sensei.
One day, he and Yahiko were attacked by a renegade ninja from Iwagakure. The enemy was about to murder Yahiko, and Nagato used his Rinnegan to kill him. After seeing Nagato's dojutsu, Jiraiya agreed to teach the three orphans ninjutsu and privately confided in Nagato that he viewed him as the reincarnation of the Sage of Six Paths, who wielded the Rinnegan and spoke of obtaining peace and understanding one another. Nagato decided that he would use his power to protect Yahiko and Konan, and after they had gained enough strength to thrive on their own, Jiraiya had left them.
Yahiko, Nagato and Konan travelled throughout Amegakure and recruited various Ame-nin into their newfound organisation, which would find a peaceful solution to the war. Hanzō, the ruler of Ame, perceived them as a threat and invited them to a negotiation under the pretext of quelling the conflicts between the lands of Fire, Wind and Earth. However, the meeting was a trap, consisting of Hanzō's men and Danzō Shimura, a commander from Konoha, who supplied his Root members for the ambush. Hanzō held Konan hostage and threw a kunai to Nagato, ordering him to kill Yahiko in order to save himself and Konan. As Nagato picked up the kunai and hesitated, Yahiko threw himself onto the blade, telling Nagato to protect Konan and that Nagato was the true saviour of the world. Satisfied that Yahiko was dead, Hanzō ordered his troops to kill Nagato and Konan as well.
Enraged, Nagato summoned the Demonic Statue of the Outer Path, using it to engulf the souls of Hanzō and Danzō's men and killing them, although at the cost of having black chakra receivers stabbed and embedded into his back, becoming permanently emaciated from the effort.