How does chess.com calculate accuracy?

No one knows. They haven't revealed it. A search of the forums will reveal much speculation.

They pull a number out of a hat? I had a game earlier where I scored 99% accuracy and it said I had 5 blunders and 3 mistakes on the post game screen
I haven't seen one like that. Please post a link to the game.
Gosh. You are 42-0. It would be interesting to see such a game. I took a look and saw a couple of 99 accuracy games but didn't find that one.

Accuracy isn't based on best moves divided by total moves. It is based on the cumulative effect of calculating the "centi-pawns" difference from each move versus zero; zero being every move is perfect so there is no difference. I also don't think Book moves are a factor. The number of moves is likely a factor, as well; however, the math is beyond me. Your accuracy is higher when the difference between the sum of each move's difference from "perfect" is very small. So a game with a lot of Best and Good moves should have a high accuracy rating, even with some inaccuracies, or even a mistake; whereas a game filled with mistakes and blunders is going to have a very low accuracy rate.
In your posted game, whenever a move was made that isn't Best (or Book), you see the numerical difference between the move as made versus the best move. For example, White's third move, c3, was "Good" and resulted in a "score" of -.06, but the Best move would leave the position as +.20. That is a difference of .26; these differences add up. It is the sum of those differences placed into a formula that determines accuracy. An average per move is determined.
Clicking on "Details" from the report shows that the average difference per move for Black in centi-pawns was .22, which is pretty good (accurate); White's average per move was 3.06, pretty bad.

That 99 accuracy doesn't mean you are playing 99% accurately, it just means you are playing 99% like a computer. Something to think about as you continue your 'career'.

98.5 vs 98 but I still came out on top. I'm not convinced the stat means anything
It helps to identify people like you. It is 99% accurate in predicting which accounts get closed.

It seems to be very close to (best+excellent moves)/(total moves excluding book). There seems to be some deviation, though, which could be related to how "excellent" and excellent move is. But seems roughly right.

I was wondering about this as well, so out of curiosity I ran an analysis on today's Skilling open game between Magnus and Nepo, the one where Nepo blunders a full knight out of the opening. He still got 96% accuracy! I think there's something more to this, e.g. the computer has does some machine learning analysis to take into account what moves are more difficult to find for a computer. Very confusing though
I dont use "accuracy" as an indicator of how well I played. If you navigate to the Details tab there's a Best move % metrics. I find this a bit more accurate and when you see it, you will realise even though the accuracy may read over 90%, the best move % could be lower than 50%. I consider it a better indicator of "accuracy".
I also look at the engine evaluation and whenever it goes down I consider it a subpar move, even when my accuracy rating is overrated for the match.
98.5 vs 98 but I still came out on top. I'm not convinced the stat means anything
It helps to identify people like you. It is 99% accurate in predicting which accounts get closed.
So according to your logic, and considering my ELO rating, in my last 7 games there's a HUGE probability I was cheating using an engine? RIIIIGHT. Except if you look at the best move % metric in the Performance tab you will see i only match the best move in around 50% or less of the match.

You don't have to always hit the "best" engine move and consider everything else sub-par. Engines are no gods, and their evaluations are often flawed (or they wouldn't lose to other engines). It's just how they work - they go to some depth and try to judge there. Anything that happens beyond that horizon is not really considered. So if an engine says a move is +0.2 and you play something that's 0.05, you know what? Your move might eventually prove to be better.
What engines are good at is detecting huge blunders. You lose a couple of pawns or a piece. Because even a pawn down, if you don't look at the analysis to the end, it might be a drawn endgame.
So I think it makes sense for "accuracy" to look at more than just the best move.

Just found this thread. I speculate that each move's +/- pawn value is compared to what it deems "best" and it then averages those differences over the number of moves played. A blunder would lose more pawn fractions than an inaccuracy...
speaking of accuracy look at this game
A perfect game of chess always ends in a draw.

speaking of accuracy look at this game
A perfect game of chess always ends in a draw.
It is also my current belief that a "perfect" chess game would also end in a draw (due to not being able to force a win, even with slight advantage, due to the Principle of Two Weaknesses). However, the "perfect" chess game resulting in a draw has not been proven; you shouldn't declare "always ends in a draw" as if this is a proven fact. As incredible as current chess computers are, they are far from solving chess.