For Mac OS, the reason is mainly historical. In Mac OS 7, the menu bar did not have a clock:
Mac OS 7.5 added a clock to the menu bar, and the best place to put it that didn't require any user retraining was on the right side of the menu (note that it does not displace menu items present in Mac OS 7).
You could also argue that this is because before Mac OS 7.5, the popular shareware program SuperClock that added a clock to the upper right corner did it solely because that was the only place the could prominently place it. When Apple "sherlocked" the functionality for 7.5, they made it identical to how SuperClock displayed the clock, right down to the position in the menu bar.
It is possible that Windows 95 followed this right side of screen convention established by Mac OS, although it's also possible that they independently decided to put the clock in the opposite side of the Start Menu in the bottom left corner, with left being the most prominent side for users of left to right languages. I'm afraid I don't have any evidence either way for the reasoning behind placing the clock there.
After the precedent was set for the clock being on the right side of the screen for both Mac OS and Windows, it seems that the established convention is largely what keeps it in that place on desktops, although it doesn't necessarily explain why it is in that location on mobile devices.