As you know, the highlight of the ritual is throwing beans at the oni. This specific activity is known as mame (魔滅), meaning "to destroy the demon" (魔を滅する, ma wo mes suru). Alternatively, mame can also be rendered as 魔目, meaning "demon's eyes". So to shoot the demon's eyes is mame wo iru (魔目を射る).
The Japanese word for beans is mame (豆), and to roast beans is mame wo iru (豆を煎る).
In other words, it's a pun.
Incidentally, the roasted soy beans are known as fukumame (福豆), meaning auspicious beans.
In folklore, the custom is attributed to a story of a demon from Mount Kurama, who ravaged nearby villagers. His reign of terror was finally put to a stop when Bishamonten revealed himself, and banished the demon by throwing roasted beans at its eyes. To this day there is a temple on the mountain, Kurama Dera dedicated to Bishamonten.
On a theoretical level, the Japanese believed the "Five Grains" to each house s "grain spirit", and as such possess great ritual power. Rice were the most powerful, followed by soy beans (known simply as "great beans" (大豆) in Japanese).
In Japan, azuki and other beans are symbolically powerful . . . Beans have long been valued both for nutrition and for ritual power.
Foster, Michael Dylan. The book of yokai: Mysterious creatures of Japanese folklore. University of California Press, 2015.
However, it doesn't have to be soy beans. The Shinetsu region customarily uses shelled peanuts, for instance. It seems this is because the setsubun ritual ends with participants picking up the beans and eating as many beans as their number of years. For obvious reasons, it is more hygienic to use peanuts.
Anyway, there is technically no reason it has to be soy beans and not other types of beans. Some regions historically used red beans, and some temples use rice in similar rituals. It is likely that the ritual defaulted to soy beans due to their their widespread cultivation - and because their size makes clean up easier.