The man was travelling on a train going towards Malmö together with the boy, who was in tears.
Several of the passengers noticed the boy and alerted train staff. They described the man as strange. The boy was crying for his mother and did not appear to know the man, writes newspaper Sydsvenskan.
When the train stopped at the station in Landskrona, the man and the boy got off. A train attendant, who had attempted to speak with the boy to ask him if he knew the man, quickly followed.
"I ran after them and yelled at him to let the boy go. I then got hold of the boy's arm and after a while the man let go. I then yelled at the man to stay where he was and he actually did," she told Sydsvenskan.
Police were called and arrested the man, who had no previous convictions and was not known by police.
He is however now suspected of child pornography offences on two occasions, an aggravated child pornography offence (which relates to spreading such material) and raping a child.
The suspected rape is said to have taken place in the man's home in central Landskrona in December 2016.
"These suspicions did not exist before the arrest. It was something we discovered during the investigation that has been carried out since," prosecutor Eleonora Johansson told Sydsvenskan.
The prosecutor declined to comment on whether or not the man and the boy on the train knew each other, but praised the train attendant: "It's thanks to her intervention that we were able to arrest this man."
The train attendant told Sydsvenskan she had no choice but to act. "He was so tiny and desperate, I had to do something. I cried with him. No child should have to experience something like that."