In a replica set, the servers will communicate with each other and decide on a machine to become primary. Any of the machines in the cluster can become primary.
Whichever machine is primary will stay the primary unless something happens which means it can’t be primary any more, such as a reboot, or a call to
stepDown. At that point another machine in the cluster (if possible) will be elected the new primary.
At this point, the same rules apply - the new machine will stay primary until something happens to stop it.
The first option to
stepDown is the amount of seconds during which the primary you are stepping down is not allowed to be primary, but that doesn’t mean it will automatically become primary again after 20 seconds.
When you’ve called
stepDown(20) you’ve forbidden the primary you’re connected to from becoming primary again for 20 seconds, but unless a new election happens after that time, the machine will stay as a secondary indefinitely.
Here’s the documentation for this call, in case you need more details.