As a basic rule, you are subject to the legislation of the country where you actually work as an employed or a self-employed person. It doesn't matter where you live, or if you are EU or NON-EU citizen. Howerer as an NON-EU citizen it requires proper work-permit.
If you work in a different EU country from the one where you live and you return to your country of residence daily, or at least once a week, you are a cross-border worker (so-called "frontier worker"). The country where you work is responsible for your social security benefits.
As a cross-border commuter - whether you are employed or self-employed:
You pay social security contributions to and are covered by the EU country where you work (Denmark).
If you lose your job, you should apply for benefits in the country where you live (Sweden).
You can also find this information here:
Hope this answer your question. Otherwise write a comment.