Yes — magnesium and vitamin B12 aren't known to interact with each other and are commonly taken together, but stay within sensible doses and check with your pharmacist, especially if you take other medicines.
Authoritative sources don't list any direct interaction between magnesium and vitamin B12. The NIH Office of Dietary Supplements names B12's relevant interactions as drugs that lower its levels (metformin, and stomach-acid reducers like PPIs and H2 blockers) — not magnesium — and B12 has no tolerable upper limit because of its low toxicity, with the body absorbing only a small fraction of high oral doses. The NHS's medicine-interaction guidance for B12 (cyanocobalamin) likewise flags metformin, antacids, H2 blockers and proton-pump inhibitors, not magnesium. Magnesium's documented interactions are with diuretics and certain antibiotics, again not with B12, so the two are not expected to clash when taken together.
Watch the magnesium dose, not the B12: the NIH upper limit for magnesium from supplements (and laxatives/antacids) is 350 mg a day for adults, and going over it commonly causes diarrhea, nausea and stomach cramping, while extreme intakes can cause an irregular heartbeat — so a magnesium plus B12 combo product or stack should keep supplemental magnesium at or below 350 mg unless a doctor directs otherwise. Magnesium can also bind certain antibiotics (tetracyclines like doxycycline, quinolones like ciprofloxacin/levofloxacin) and should be separated from them by at least 2 hours before or 4-6 hours after, and it interacts with diuretics. B12 itself is well tolerated even at 500-1,000 mcg doses, but B12 absorption is reduced by metformin and long-term acid-suppressing drugs. People with reduced kidney function should be cautious with magnesium and check with a clinician first, and anyone on prescription medicines should tell their doctor or pharmacist about both supplements before starting, since supplements aren't tested for safety the way prescription medicines are.
This is general reference, not medical advice, and not a guarantee of safety. Interactions depend on your doses, health conditions, and other medicines. Always confirm with your pharmacist or doctor before combining products, and follow the dosing on each label.