I'll answer about India, and Indian Railways.
While in India, you can't call it legally approved to be okay to walk on railway tracks. I'd say the way things are taken is depending upon where you are. Officially, trespassing any of the Railway property without being a passenger on a train or an authorized person to be there is illegal.
Its quite unfortunate to say, but with Indian Railways the things aren't so straight, There are Gray shades between how Law and Order are maintained in Indian Railways. For example, a plastic garbage collecting guy who is not an employee of Indian Railways is allowed to walk through and along the tracks to pick up plastic waste, mostly water bottle which are recycled. (Why such a job is needed is a question worth not wasting any time here), Whereas a Railway Gang-man, an employee of the Indian Railway is supposed to be walking on railway tracks to ensure the track are intact and safe. Its his job.
I have walked on tracks over longer distances because there isn't any alternative to it unless I was ready to walk twice the distance. During that walk I came across a couple of Railway sub-section offices, traction centers and a few Railways officials too. They looked perfectly okay with me walking along the track, but it doesn't mean it is legal to do so.