Why was castling bad for white in this game, and engine strongly prefered trading queens? I'm a novice player, trying to improve. In the following game I played as white recently on chess.com, I analysed afterwards and my worst move of the game was apparently when I castled. The engine strongly favoured trading queens on move 9.

[FEN ""]

1. e4 e5 
2. Nf3 d6 
3. Nc3 Nf6 
4. d4 Nc6 
5. Bb5 Be7 
6. Bxc6+ bxc6 
7. dxe5 dxe5 
8. Nxe5 O-O 
9. O-O Bg4 
10. Nxg4 Nxg4 
11. Qxg4 Bb4 
12. a3 Bxc3 
13. bxc3 Rb8 
14. Be3 a5 
15. Bd4 Rb4 
16. Qxg7# 1-0


Why is this? I can't quite see why the trade is beneficial, and so much better than castling at this point.

Maybe I'm giving up initiative by allowing black to initiate a trade? Or perhaps I'm better developed, so it makes more sense to trade down and benefit for more activity with my pieces? It was a strong 2 point difference from what seems like a fairly even choice from my perspective.

Any ideas why the castling move at that point was such a bad choice?
Castling (9. 0-0) is not a very bad move. You still have a very solid advantage after it due to being a pawn up and having the better pawn structure (black's pawns on a7, c7, c6 are weak and vulnerable).

However you had a better move...

The pawn on c6 is hanging and you could have simply taken it. Both 9. Nxc6 or taking the pawn after queen trades (9. Qxd8 Bxd8 10. Rxd8) are good.

Taking the pawn after queen trades is better, because you will also exchange the knight later on for black's dark squared bishop (which is due to the pawn structure, the stronger piece).

For instance an almost forced sequence could be:

[FEN "r1bq1rk1/p1p1bppp/2p2n2/4N3/4P3/2N5/PPP2PPP/R1BQK2R w KQ - 1 9"]

9. Qxd8 Bxd8
10.Nxc6 Bb7
11.Nxd8 Rfxd8
12.f3


In the end white is left with two pawns up, a strong pawn structure and the better bishop.