Socialism and Marxist movements were at base about freedom from class and capitalist oppression. Countries like Italy, Germany or France were nation states in the early and mid 20th century, therefore socialist activity was focused on questions of class and in changing the government of the existing state, as the oppressors were generally not seen as foreign.
However in this period much of the world was part of some kind European empire, and other parts of it, like China, were under strong European informal influence. Here oppression was intrinsically tied up with foreign rule and foreign control of industry. This is dealt with quite extensively in Marxist writing, perhaps most famously in Lenin's 1917 work 'Imperialism, the Highest Stage of Capitalism', which claims that imperialism is a competition between advanced capitalist countries for markets and natural resources in the rest of the world.
Indeed after the Bolsheviks took power in Russia in 1917, they were initially (at least outwardly) pro-independence for the constituent parts of the Russian empire that desired it. The Declaration of the Rights of the Peoples of Russia was issued 15 November 1917, allowing secession for the peoples of Russia, and subsequently many parts of the empire did declare independence, only to be forcibly reincorporated into the USSR in the following years.
Socialist and communist parties had also taken the lead in fighting against foreign occupation in WW2, in Greece and Yugoslavia for example.
So ideologically, Marxism and socialism did fit extremely well with national independence against foreign, capitalist oppressors. At the time it was THE ideological alternative. Our perceptions have perhaps been coloured today by the fact that the Soviet Union itself later turned into an oppressive empire of its own.
This answer has just focused on the ideology, there's also the factors of socialism being useful for nationalist movements to gain the support of the people, by promising a better future, and of potential aid from the soviet union, which maybe others can expand upon.