The incident occurred last night on the Ernakulam-Chennai Suvidha Express soon after the train left Palakkad junction, police said.
At around midnight, the 23-year-old woman raised an alarm that the duo, who were occupying the berths opposite her, had tried to molest her, they said.
The co-passengers, who woke up on hearing her cries, caught the two men, beat them up and handed them over to railway police after the train reached Coimbatore station, police said.
The two men were handed over to railway police in Palakkad this morning as the incident occurred near that station, police said.