Don't freak out.
Having an orgasm is a response to stimulation. Our bodies can get used to a certain kind of stimulation (whatever it is) and not having that specific stimulation can prevent one from getting close, let alone going over the edge. A lot of it just depends how "into" what we're doing we are. So there are a lot of questions:
How sexually experienced are you? How long had it been since you'd had an orgasm? Have you ever had an orgasm with someone doing other things, like oral, or mutual jo? When you masturbate, are you watching porn? Do you grip really hard and jerk yourself really fast? How sexually excited or turned on were you by your partner or by the idea of fucking him? Had you spent a lot of time -- or at least sufficient time -- in foreplay or did you pretty much just get right down to it?
The point being there can be any number of reasons why you couldn't reach orgasm during anal sex. For example, if you're body is used to looking at or watching porn and used to the rapid sensation of a tight grip, and/or you weren't sufficiently 'into' the idea or fantasy of fucking your partner, then it is perfectly understandable that you wouldn't 'get there'.
So, it isn't like there is something wrong with you -- its just your body hasn't yet associated that sensation with cumming.
I think it isn't uncommon for young guys to think that sex is just about getting off. Usually when young guys masturbate, they try to get it over with more or less ASAP. This may be because the opportunity for privacy could be brief so you learn to do it quickly.
So, my first suggestion is that you try to think about sex as something slightly different than an opportunity to just "get off". Yes, it *can* be that -- like having a quick blow job in a closet. But it doesn't have to be that way. It can be an opportunity to stimulate and get stimulated by a partner -- to in a sense 'edge' one another for as long as possible doing whatever feels good until you just *have* to explode all over the place.
You don't need to cut masturbation out of your life but you may want to cut back on the number of times you masturbate, or be sure not to masturbate for at least 24 hours before you have sex. You might want to change up what you do when you masturbate. Like, only use your imagination (no porn). Have you ever used a fleshjack? It is a different sensation. (I've heard it is possible to make variations of this at home but I've never done it so can't guide you on that prospect.) Have you ever fantasized about fucking as you masturbate and tried keeping that sensation close to what it would be like while fucking? IOW, thrusting your hips more than using your hand, varying the speed of your thrusts or your hand, having a different grip? Or, maybe you're more of a bottom than a top? Have you ever fantasized being penetrated?
Personally, there are some things that "get me off" more than others and the way I have sex is to just enjoy pleasuring my partner and all the different sensations and 'fantasies made real' which includes making out and foreplay, touching and being touched, looking, licking, tasting, smelling, sucking, biting, scratching, etc., etc., for as long as possible -- and *then* do whatever is necessary to have an orgasm. IOW, it isn't just about getting to orgasm ASAP.
.