In the latest mass kidnapping in Nigeria, gunmen attacked the rural train station in southern Edo State earlier this month, abducting dozens of passengers.
The train station abduction highlighted the spread of insecurity in Nigeria, which will be a major issue for voters in next month's election to replace President Muhammadu Buhari.
Edo State communication commissioner Chris Nehikhare said in a statement on Wednesday the last two hostages had been rescued and seven people detained.
Two of those were local traditional chiefs.
Originally, 32 hostages were reported captured but that figure was later adjusted to 20 victims. Several were rescued earlier this month.
Kidnapping for ransom is a major problem in Nigeria.
It is most prevalent in the northwest states, where heavily armed bandit militias operate, but violence has spilled over to other regions.
In one of the country's most high-profile attacks, in March last year, gunmen with explosives blew up the tracks and raided a train travelling from the capital Abuja to the northwestern city of Kaduna.
Eight people were killed and dozens more kidnapped and held for months. The train service only resumed in December 2022 after the final hostages were released.
Buhari, a former army commander and one-time military ruler, had promised to make Nigeria safer. He steps down after two terms in office.
Jihadists fighting a 13-year-old war in the country's northeast have been pushed back from the towns and territory they once controlled.
But they are still battling the armed forces and attacks have spread, including in the northwest, where criminal gangs target schools, communities and highways in kidnapping and looting raids.
Often dozens of people are kidnapped together and taken to hidden camps deep in vast forests spanning northwestern states, while their captors negotiate ransom payments.