It isn't quite legal -- it's just customary to pretend it is.
The dealership situation in the US is a good comparison -- Tesla refused to comply with the parasites in the American system and set up dealerships. Tesla managed to sell on the internet or in neighbouring states while fighting to get many of the dealership protection laws overturned. They did indeed succeed in getting some of them overturned (deemed illegal), they succeeded in getting others interpreted differently (which means that the customary interpretation was illegal), they succeeded in getting yet others changed to be less extortionary.
That fight is not quite over yet. An interesting wrinkle is that it is not actually in Tesla's interest to get rid of all those laws, as long as they are more of a bother to the competition than to Tesla ;)
Some of the stuff that is happening in Sweden/Denmark/Norway/Finland is completely legal -- some of it is even fair and reasonable. Some of it isn't and relies on selective enforcement of laws or on customary but incorrect interpretations. Some of it is according to laws that are against constitutions.
I think Tesla has a good case here, too. Tesla can fight dirty (but within the law) by making the unions unpopular AND by making it extremely clear to foreign investors that these countries don't have all that much rule of law in certain areas and are therefore risky places to invest. I wonder who happens to own a really, really big platform for spreading that kind of information...