I'm answering this from a (geo)political perspective. If you want a more legally focused answer then I would suggest asking on Law.SE as well. In this case, I'm considering what a country is allowed to do to foreigners from an international law perspective and what other countries can do in retaliation to a country crossing the line.
Generally speaking I would say that Belarus has no authority to prevent a person from leaving the country. Whether someone leaving Belarus is allowed entry into another country is not Belarus' business. It might be Belarus' business if they have some complaint against you, but that should not be arbitrary. Article 9 of the Universal Declaration of Human Rights states:
No one shall be subjected to arbitrary arrest, detention or exile.
Specifically, the right to leave one's country, or any country, is a human right. As article 13(2) of the Universal Declaration of Human Rights states:
Everyone has the right to leave any country, including his own, and to return to his country.
To answer your question more literally, let's have another look:
On what basis can a 3rd country can legally forbid a foreigner from leaving the country if there is no accusation of crime?
So we've seen the international law perspective above. It's a violation of human rights to prevent someone from leaving when there are no good reasons for doing so. That said, there is a difference between having international law on your side and being treated in accordance with international law. For example, the US State Department warns US citizens against traveling to Belarus:
Belarusian authorities have detained tens of thousands of individuals, including U.S. citizens, for alleged affiliations with opposition parties and alleged participation in political demonstrations. U.S. citizens in the vicinity of the demonstrations have been arrested. Some have been victims of harassment and/or mistreatment by Belarusian officials. The Belarusian authorities have targeted individuals associated with independent and foreign media. On May 23, the Belarusian authorities forced the landing of a commercial aircraft transiting Belarusian airspace in order to arrest an opposition journalist who was a passenger.
Belarus enforces special restrictions on dual U.S.-Belarusian nationals and may refuse to acknowledge dual U.S.-Belarusian nationals’ U.S. citizenship, including denying or delaying U.S. consular assistance to detained dual nationals.
It seems that they are aware of Belarussian violations of human rights, but there are limits to what they can do. In your example, I would say that complaining to Polish and Russian authorities can't hurt (but whether they can help is another matter). To be safe though, I would recommend following the relevant travel advisories. I am not sure about the Russian and Polish advisories, but if you can avoid travelling through potentially dangerous countries then that's less risky.
The real world aspect to the question is that there's little you (or your lawyers or your own government) can do when it does happen. For example, James K wrote the following in answer to a question about the arbitrary arrest and detention of a British woman in Iran (partial quote):
Zaghari-Radcliffe is just a random woman. It's harsh, but she's not worth that much to the British. She isn't worth either the cost in lives of those special ops sent to rescue her, nor the diplomatic cost of an act of war.
From that point of view, I would say that a country can prevent a foreigner from leaving even without an accusation of a crime and the decision can be completely arbitrary. Of course, the crux in your question is the word legally. We know that it's not legal by the standard of international law, but a sovereign country can choose to ignore that, as Belarus does.
The (geo)political aspect to this asks what others (your lawyers, other countries, international organizations) can do to prevent a country from ignoring international rules, like human rights. In international relations there are two categories of solutions:
soft power, which is diplomacy. By talking to the offending country it may be possible to persuade them to act in your favor. This works well if relations are friendly, because failure to resolve things amicably hurts both sides. The friendlier the countries are, the more they stand to lose if the relation turns sour.
hard power, which is about putting pressure militarily or even economically to force the other into submission. This is quite a big leap from soft power because these are not friendly moves.
Economic sanctions are the simplest tool but they act slowly and they require that many countries participate so that the target actually feels the economic pain. These tools may also be ineffective because they can harm the general population as well as the leadership which you want to punish.
The last option is to go to war. This is both costly (financially and w.r.t. loss of life), legally questionable (unless you have a good reason, you will be seen as the agressor), and unpopular. As such, countries are very reluctant to go to war over minor issues.
As you can see, both soft power and hard power are mostly tools employed by countries. It's not something a lawyer can do, other than by asking a country to consider it. In the case of Belarus, some countries are pressuring the country through sanctions. This is actually for a whole host of reasons which are detailed in that Politico article.
TL;DR
International law forbids a country from preventing people from leaving unless there are good reasons to detain them.
A country can ignore international law.
Forcing countries to abide by international law is hard. It is up to the international community to force countries to abide by it, either diplomatically or through force.