Is the default orientation for mobile devices hardware or software specific?

I have noticed that when switching between one mobile application to another, sometimes the orientation of the user interface changes so that I have to rotate the screen 180 degrees so that it is facing the right way. I assume that if the software or hardware recognizes the direction that the user is holding the device, or at least the orientation that the most recent application is set at, then it should not change the orientation.

This leads me to wondering if there is actually a default orientation for devices which is configured in the hardware, and if there is also some configuration of the software as well (and that they sometimes are configured to unintentionally clash).

Is there are default orientation in landscape view for mobile devices? And if so do they exist in hardware and/or software configurations?