Sleep allows you to rest, the effect of spells depends on the spell
In general, you probably could benefit from a long rest if sleeping for 8 hours, for whatever reason. The definition of a Long Rest (p. 186, PHB) is:
A long rest is a period of extended downtime, at least 8 hours long, during which a character sleeps or performs light activity: reading, talking, eating, or standing watch for no more than 2 hours. (...) A character can’t benefit from more than one long rest in a 24-hour period, and a character must have at least 1 hit point at the start of the rest to gain its benefits.
There is nothing in there that says the sleep must be natural. As long as you have at least 1 hit point, and have not taken a long rest within the last 24 hours, and you sleep you should be able to benefit.
However, there is some explicit rules text supporting a DM who does not want to allow this. The introduction to Resting (p. 186, PHB) says:
Adventurers can take short rests in the midst of an adventuring day and a long rest to end the day.
So, is it the "end of the day" if you get hit by magical sleep in the midst of the day? Up to the DM. Most groups I know allow long rest not only at the end of the day, but at any time, because you cannot benefit from more than one in 24 hours, so there is little chance for abuse. In those cases where you get hit by a very powerful, long term sleep effect, you can probably use all the help you can get.
Note that there are additional rules from Xanathar's Guide to Everything (optional, so up to the DM too), about sleeping in medium or heavy armor (which you have no time to doff, if you get put to sleep magically):
When you finish a long rest during which you slept in medium or heavy armor, you regain only one quarter of your spent Hit Dice (minimum of one die). If you have any levels of exhaustion, the rest doesn't reduce your exhaustion level.
What spell is this about?
There are actually very few spells in the core rules to which this could apply, so the question feels a bit hypothetical. The following spells in the PHB can put a character to sleep:
- Sleep (obviously). This one last only for 1 minute, so does not apply here.
- Eyebite (Asleep option). Again, lasts only up to 1 minute, so does not apply.
- Symbol (Sleep option). This one lasts only for 10 minutes, so does not apply here.
- Dream does not apply, as it does not put anyone to sleep, it works on someone already sleeping. If you chose the nightmare option, they will not benefit from a long rest, which implies they can get a normal long rest from their sleep under the normal version of dream. As @Kryomaani points out, the fact that this spell has explicit language to state when the target will not benefit from a long rest strongly supports that other spells would need to say so explicitly too, to have that effect.
- Imprisonment (Slumber option). This is the only one that seems to apply, as its duration is until dispelled. It has no text about preventing long rest benefits.
- Wish The risky off-label version, as Imprisonment is 9th level and thus cannot be cast with a standard wish. If the DM agrees to grant a wish for long sleep that prevents rest, this could achieve it.
So, outside of some homebrew, you have to get hit with a 9th level spell to have this problem.