During World War II most countries preferred to use domestic designs over foreign ones. Most major nations had their own rifle designs, their own plane designs, their own ship designs, etc. So what about the Swedish Bofors design was so appealing that the Americans and British decided to use it over domestic alternatives?
In a nutshell, it was an excellent gun. Though domestic designs were generally preferred, during the frantic rearmament of the late 1930s the British procured several foreign cannon including 20mm guns from Oerlikon and Hispano-Suiza and the 40mm Bofors (also e.g. the Czech ZB-53 machine gun as the Besa, retaining its 7.92mm calibre). After several other countries had opted for the Bofors, in 1937 "Britain chose it, seeing no point in wasting time designing a gun when a perfectly good design was available off the shelf" (Ian V. Hogg, Allied Artillery of World War II), obtaining guns and a licence for production.
The Royal Navy had a 40mm anti-aircraft gun from the First World War, the 2-pounder "pom-pom", often employed in quadruple or even octuple mounting; the Bofors had superior performance, though, so was also introduced for naval use. A stop-gap twin land mounting for the 2-pounder weighed almost eight tons and was outperformed by a single Bofors of about a quarter of the weight.
Likewise in the US, though considerable effort was devoted to the M1 37mm AA gun it was impossible to overlook the superior performance of the Bofors. Initial approaches were made in late 1937 but stalled, apparently due to a misreading of a telegram outlining the costs of a trial gun and ammunition (see History of the Americanization of the Bofors 40mm Automatic Antiaircraft Gun). Following the outbreak of war examples of guns were obtained from the British and Dutch, and both the Army and Navy adopted the weapon, manufacturing agreements being reached in 1941.