You didn't state which Magic KB you bought or your iMac model (though it must be fairly recent if you have Mac OS 14). Might your purchase be the new-ish keyboard with Touch ID? This is for silicon Macs (2020 onwards).
An older keyboard is compatible with various Mac models, 2013-23: Magic Keyboard - US English - Apple
and the compatibility dropdown lists these iMacs:
- iMac (24-inch, M3, 2023)
- iMac (24-inch, M1, 2021)
- iMac (Retina 4K, 21.5‑inch, 2019)
- iMac (Retina 4K, 21.5-inch, 2017)
- iMac (Retina 4K, 21.5-inch, Late 2015)
- iMac (Retina 5K, 27-inch, 2019–2020)
- iMac (Retina 5K, 27-inch, 2017)
- iMac (Retina 5K, 27-inch, Late 2014 - 2015)
A Version with a numeric keypad is available also.
Did you seek any support for your old KB? An Apple Store might be able to revive it for you.