Your question is similar to many some bring up - they look at these incidents in the Old Testament and ask the inevitable questions. Your question relates to what Ezekiel saw. But to fully understand this, you really need to have the foundation of what the Old Testament reveals prior to this, parts of which I need to briefly summarise. (So will lack important in-depth background understandings).
The other aspect to understand this incident may? also require putting aside some of your current understanding of the Old Testament. Nevertheless, let’s look a little closer ... first some background ...
Ezekiel was a prophet for Judah, the southern kingdom. The nation of Israel had split into two. A ‘prophet’ was someone who interacted with the ‘spiritual realm’. And much of Ezekiel is ‘looking’ into this realm. It is important to keep this in mind.
Judah was in captivity, in Babylon. King Nebuchadrezzar had ransacked Jerusalem, the temple, then returned later and took [many/most] of the nation into captivity - including Ezekiel. But what’s important to ‘take’ from this is the reasons why. Why was Judah ‘exposed’, ‘unprotected’ - even though their God was Almighty God, Yahweh. Reason - the Law. The nation was ‘under the Mosaic Law. Captivity because they violated the Sabbath. Desecration because they began to worship other gods. And because of this, they took themselves out from under Gods protection.
DEUT 28:45 Moreover all these curses shall come upon you and pursue and overtake you, until you are destroyed, because you did not obey the voice of the Lord your God, to keep His commandments and His statutes which He commanded you.
Deuteronomy 28 starting at verse 15 has a long list of consequences. Some very ‘heavy’ consequences! You should read these! It will help towards understanding what happens in Ezekiel 8/9.
Some other background that would help is realising that being ‘put’ under the Law was not Gods idea - but that’s outside of being needed for this answer. The captivity, Judah being taken out of Judah, and put under a Nation with other gods was a ‘life preserver’ - doing this, it ‘shielded’ them from those consequences listed in Deuteronomy.
But what you read in Ezekiel 9 is for those who weren’t taken captive! Those who stayed (the reasons are interesting, eye opening, but outside of this answer.). Nevertheless, what you read in Ezekiel, the horrific judgements, were exactly those listed in Deuteronomy. And because they were ‘under Law’, God had no choice but to permit them.
Violating the Law was serious! It ‘killed’ thousands! A man picking up sticks! There was zero mercy, zero tolerance. The Law was not Gods idea! But God could not stop the judgement - the penalty was demanded by an Accuser.
In Ezekiel 9, we see the ‘background’ (spiritual realm) to what was described in Ezekiel 8. The ‘men’ assigned to ‘kill’ were spiritual entities - angels. Just like we see throughout the Old Testament - the firstborn in Egypt, Sodom, Lot, David in 1 Chronicles 21. Destroying Angels. They, ‘the angelic executioners’, came from the way of the upper gate - the Northern gate. All ‘spiritual oppression’ comes unto Jerusalem from the North.
The instructions to these ‘men’ were to carry out the requirements of the Law, the demands of the Law. The Israelites were warned repeatedly that there was no leeway - no tolerance!
EXODUS 23:21 Beware of Him and obey His voice; do not provoke Him, for He will not pardon your transgressions
So those that were judged in Ezekiel 9 were judged under the Law - not by God. They were there (not exiled) because they had ‘rejected’ God, were worshiping idols - in the temple!!!. They had taken onto themselves the consequences of the Law. There are other significant factors that contribute to their ‘status’, to them being the [seemingly innocent- but not] recipients of judgement, but you’d need a book to outline this. Sufficient to say that at this stage, they were ‘reprobate’ - (Romans 1 has a description of this state.).
Summary - There are clear understandings to these Old Testament incidents, but they are not necessarily found in the ‘traditional doctrinal explanations’. There is not much comfort in those explanations - but the Bible does have satisfying explanations, they are there - ones that show what was really behind these - and importantly, ones that reflect the same God you see in the New Testament. But a forum answer can’t reveal these, you need a book.