Police said five inmates and two guards died in the attack near the town of Kalutara. Nine others are in hospital.
The bus was transporting a prominent underworld suspect and other prisoners to court.
Police say the shooting resulted from a feud between two gangs. The gunmen escaped after spraying the bus with bullets.
Among those killed was Aruna Udayashantha, alias Samayan, who police say was a major underworld figure.
The BBC's Azzam Ameen in Colombo says the shooting is the single deadliest of its kind in Sri Lanka for years.