In many country, the elected leader can only run for 2 mandats (terms) in a row.
I'm curious of the reason behind that.
You might be interested in this list of political term limits across the world at this Wikipedia link. However there are still 54 Presidential and Prime Minister offices in the world that do not impose strict term limits. The balance is split fairly evenly between Africa, Latin America, Europe and Middle East.
As you rightly identify, the rest of the world using a democratic system of Government imposes some form of term limits on leaders and elected representatives.
A term limit is a legal restriction that limits the number of terms an officeholder may serve in a particular elected office.
When term limits are found in presidential and semi-presidential systems they act as a method to curb the potential for monopoly, where a leader effectively becomes "president for life". This is intended to protect a democracy from becoming a de facto dictatorship. Sometimes, there is an absolute limit on the number of terms an officeholder can serve, while, in other cases, the restrictions are merely on the number of consecutive terms.
Both Ancient Greece and Ancient Rome had term limits in their respective democracies and republics. The mercantile city-state, Venice, also had term limits.
With consecutive term limits, a legislator is limited to serving a particular number of years in that particular office. Upon hitting the limit in one office or chamber, a legislator may run for election to the other chamber or leave the legislature. After a set period of time (usually two years), the clock resets on the limit, and the legislator may run for election to his/her original seat and serve up to the limit again.
With lifetime limits, once a legislator has served up to the limit, she/he may never again run for election to that office. Lifetime limits are much more restrictive than consecutive limits.
3 famous politicians either circumvented term limits, went against the cultural norm or served prior to term limits.
The logic of term limits is best exemplified by China (which has recently abolished them again). But the original intent, implemented by Mao's successors, was to prevent another popular leader arising who could commit as many damaging and harmful policies as he had.
Mao was effectively able do implement any policy he wanted as his position as leader was unchallengeable as he had led the revolution and led the country for so long. The result was a series of catastrophic policies (like the cultural revolution) that caused great harm (from purges of opponents, economic dislocation and famine). Tens of millions died but despite this he was unchallengeable in power because of his revolutionary legacy.
To prevent anyone else going the same lock hold on permanent power that might tempt a successor to stay too long and use their position to create a personality cult that would make them similarly unchallengeable even if they pursued dangerous and damaging policies, term limits were put in the constitution. The logic of US term limits is similar (though in this case Congress were not worried so much about obviously dangerous ideas just about ideas that a popular president could enact despite congressional opposition).
There is some evidence that leaders who stay in office for too long become insulated from the normal pressures that prevent bad ideas being stopped and corruption spreading. In democracies (and other systems with limits) new leaders bring new thinking and new ideas and can reverse the stale ideas of their predecessors. Political systems where a potential leader for life voluntarily stands down (eg Mandela in South Africa and Washington in the USA) seem to have a better track record of success and tyranny avoidance than those where the leaders stay on exploiting their popularity.
This is not a complete answer. But, while the question refers to both Presidents and Prime Ministers, there are only two countries in the world that have term limits for Prime Ministers or equivalent positions with another name: China and Vietnam.
Those countries are notable because, as Communist states, in China and Vietnam the Prime Minister's power is subordinate to the power of leaders within the Communist Party. Real power and policy decision making in China and Vietnam reside mostly in the Communist party and then the parliament, whose members are all members of the Communist party, rubber stamp the party's decisions. In the case of China and Vietnam, the real purpose is "window dressing" to create the appearance of a good formal system in an institution that is really meaningless.
So, the question is really limited to Presidents, and keep in mind that in most countries that have a President, the power of the post is closer to that of a constitutional monarch (i.e. mostly symbolic) rather than a post closer to the U.S. President, who does have real independent power.
Term limit on consecutive election for a president or a prime minister is just made for a simple reason - To stop too much power falling into hands of a single person. And there are two types kinds of term limits here -
A person holding the position of President/Prime minister cannot hold the position for more than 2 terms .Like in case of USA where POTUS cannot hold his office for more than 2 terms.
A person holding the position of President/Prime minister cannot hold the position for more than 2 consecutive terms . But this provision has loop hole as in case of Russia because of the "consecutive" word - where Dmitry Medvedev was considered mostly to be a dummy president was replaced by President Putin the next election. Soo you can hold position of President/Prime minister for two terms , place a dummy president for the third term and then again go on to be elected for the next two terms. But of course This is highly undemocratic.
This check ✔simply assured that no president/Prime minister gains too much power to undermine democracy as not putting any term limits have caused disastrous affects as seen in past through multiple examples , but not putting any term limits also hasn't caused any major problem in many countries such as India -the world 's largest democracy.