Usually yes — no authoritative source describes a dangerous direct interaction between ashwagandha and magnesium, and they're often stacked for sleep and stress, but both can upset your stomach and ashwagandha carries its own cautions, so keep magnesium under its limit and check with a pharmacist first.
There is no documented pharmacologic interaction between the two: NCCIH's ashwagandha page lists interactions with diabetes, blood-pressure, immunosuppressant, sedative, anti-seizure and thyroid medications, but not magnesium, and NIH/MedlinePlus magnesium guidance lists interactions with antibiotics, bisphosphonates, diuretics and other drugs, not ashwagandha. The realistic overlap is additive side effects, not a chemical clash: NCCIH says ashwagandha can cause "drowsiness, stomach upset, diarrhea, and vomiting," and excess supplemental magnesium's hallmark effect is loose stools and other GI upset — so taken together they can compound stomach upset and, because ashwagandha is sedating, daytime drowsiness. Each substance also has its own limits: NIH's tolerable upper level for supplemental magnesium is 350 mg/day for adults, and NCCIH says ashwagandha "may be safe" only short-term (up to about 3 months), with rare reports of liver injury and not enough information to judge long-term safety.
Keep supplemental magnesium at or below 350 mg/day (the adult tolerable upper limit) unless a clinician directs otherwise, and use a gentler form if you get loose stools — diarrhea and stomach upset are the first signs you've taken too much. Watch for excess drowsiness, especially if you also take sedatives, anti-anxiety meds, or sleep aids, since ashwagandha can add to their effect. Don't take other oral medications within 2 hours of magnesium, as it can blunt absorption of some drugs. Talk to a doctor before combining if you have kidney disease (your body may not clear magnesium well — the main group at risk of magnesium side effects), liver disease (ashwagandha has rare liver-injury reports — stop and seek care if you notice yellowing skin/eyes, dark urine, or right-upper-abdomen pain), a thyroid or autoimmune condition, are on diabetes or blood-pressure medicine, are pregnant or breastfeeding, or have surgery coming up — all situations where NCCIH says ashwagandha should be used cautiously or avoided. When in doubt, run the exact products and doses past your pharmacist.
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.