Be Calm
It's only when you are able to remain calm and think, that you can successfully figure a way out of the situation. If you allow yourself lose your composure and start panicking, you might end up making mistakes that will aggravate the situation.
Call for Help
You should immediately call a reliable person you know for help. Explain your situation to them and where you are so they can help you out. If you are accidentally stranded and you don't have a phone (maybe because your battery ran down, you don't have airtime to call or you forgot your phone), you need to be a lot wiser about how you ask for help. Due to the nature of our society, there is a lot of distrust for strangers so you are unlikely to get help by just walking up to a random stranger; as a result of this, your discerning skills are going to have to be brought to fore. Assess the people around you and figure out the ones that are likely to actually help if you ask for it, because there are those who might use the opportunity to swindle you. Another option is to ask for the nearest police station or government office, and go to them for help.
Be Patient
After calling or asking for help, most people forget to be patient when stranded. If you called someone from the other side of town for help, you're going to have to accept that you'll be waiting a while for that person. Help is hardly immediate and though inconvenient, you are going to have to accept it and figure out how to keep yourself safe in the meantime.
Don't Make Your Plight Obvious
One very helpful tip for keeping yourself safe when stranded is not to make it obvious to everyone around you that you are stranded or in need of help. Once you have called for help, you need to remember that help is already on the way and you should thus try to blend in and act as naturally as possible in the meantime. This is to avoid exposing yourself to swindlers and other shady characters that might want to take advantage of your situation.