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.
There have been a number of shootings targeting prison buses.
Another suspected gang leader was injured in a prison bus shooting in the capital last March.
Police set up roadblocks after six people were killed in two separate incidents on that day, AFP news agency reports.