Railway, being a public transport system, has to balance between accessibility and speed. The deviation from the shortest route in this case is most likely to serve these places (based on local traffic demands).
The difference in this case is hardly 5 km. So, they will prefer to cover more places with a small increase in the distance.