Magic is in the domain of fantasy, not the domain of science fiction. However, there are various ways to make magic viable even in hard science fiction. Here are three of the ways:
1) We imagine that our Planet Earth exists in a universe in which physical laws are variable across vast areas of space, meaning that a science experiment has a predictable result here but a completely different result if performed a hundred galaxies away from here. We imagine that, in some regions of the universe, the physical laws are such that spells may be cast and magic will work. Furthermore, we imagine that our galaxy once passed through such a region and will pass through it again in the far future. Thus we will be able to regain the magic.
2) In the second method we imagine a cast of characters who live their entire lives in a virtual reality where magical abilities are programmed in. This method could be called 'The Matrix does Lord of the Rings'.
3) The third method is to imagine that magic always did work, except that humans don't know how to do it. Some aliens turn up from another planet and they know how to make magic work, so we learn it from them. The aliens explain that it is a natural part of how the universe works and are amazed that we are so stupid that we couldn't see it before.
I had a fourth one but I've forgotten what it was now.
The thing about getting old is you get to know lots and lots more things but you just can't remember what they are :-)