To estimate the causal effect of refugee migration on voting outcomes in parliamentary and municipal elections in Denmark, our study is the first that addresses the key problem of immigrant sorting by exploiting a policy that assigned refugee immigrants to municipalities on a quasi-random basis. We find that in all but the most urban municipalities, allocation of larger refugee shares between electoral cycles leads to an increase in the vote share for right-leaning parties with an anti-immigration agenda, and we show large differences in voters’ responses to refugee allocation according to pre- policy municipal characteristics. However, in the largest and urban municipalities, refugee allocation has – if anything – the opposite effect on vote shares for anti-immigration parties. This coincides with a sharp divide in attitudes to refugees between urban and rural populations, which may be partly explained by distinctive interactions between natives and those with different background in cities and rural areas. Refugee allocation also has a large impact on the anti-immigration parties’ choice of where to stand for municipal election, and we provide some evidence that it influences voter turnout.