There are a couple of moving pieces at play in this question, I'll try address each of them. But I'd like to start by pointing out that HellSaint's conclusion is the most actionable advice you can receive - if something in game upsets you, talk to your DM. Now, let's get to it.
Surprise makes people surprised
The way you described the scenario, it is clear that your DM did not play the surprise mechanic as it should have been played.
The correct steps are given in the rules as:
- Determine surprise.
- Establish positions.
- Roll initiative.
- Take turns.
- Begin the next round. Repeat step 4 until the fighting stops.
The reasoning given by the DM for not following these steps, appears to have been that the swarm was ready. Let's look in to that.
You do not have to be in combat to perform an action
Actions only exist inside combat. Outside combat, you just do it, no action is needed. Since no actions are ever needed outside of combat, there aren't any rules for them. It's totally unnecessary.
To be clear the idea that you can perform "actions out of combat" isn't completely true, you can perform the same effect as the action, without every taking the action.
For example if you want to chop down a tree, you don't have to use the attack action. You tell the DM your intentions, and they tell you if you need to roll, and then narrate the results.
You can be ready for something outside of combat
There's nothing special about the effects of the ready action that make it impossible to do outside of combat. "When the toast pops, I'll butter it" - why would anyone need to be in combat to do that?!
A swarm of insects could certainly be ready to do something, and when combat starts they continue to be ready. However, what has this got to do with surprise?
I rule being ready using the ready action
While there are no rules for translating pre-combat activities into combat, using actions is a sane way to do it.
When someone is ready for something, I rule they have essentially taken the ready action since that seems to be closest to the spirit of what they requested. However, once in combat they must follow the combat rules:
- The swarm is hiding in the dark (stealth check), prepared to attack whoever enters reach.
- You fail to detect the swarm (passive perception), and unfortunately walk within 5ft of it.
- Determine surprise: The swam has surprised the party. However, the alert feat stops you from being surprised.
- Establish positions: You are next to the swarm, the party is somewhere behind.
- Roll initiative: Let's imagine you roll a 20, the swarm rolls a 1.
- Take turns. First, it's your turn.
- Since you the swarm has effectively readied an action and it has its reaction available, it gets to attack. At this stage you have your reaction available too, so you could cast Shield, use Protection fighting style, use Parry, Riposte, etc.
- Now that the readied action has been dealt with, you take your turn.
Because you were not surprised you should have your reaction ready at this stage. This could potentially influence the fight. If you won initiative, you should be able to act before the swarm gets its turn, allowing you to dispatch it.
Actions that initiate combat should happen during combat
There is always discussion about when combat should start. How would you rule the following situations:
- If you unexpectedly throw a punch at someone during a conversation, does the attack land before or during combat?
- If you shoot an arrow from 600ft away at someone who is looking away from you, does the attack happen before or during combat?
Some DMs would rule that the action takes place before the combat starts (as your DM did). But I think that's the wrong way to rule. It doesn't take into account the alert feat, reactions, initiative, or other mechanisms.
This point is always controversial. While stealth vs perception is listed as a guaranteed method of gaining surprise, who is and isn't surprised is down to DM ruling at the end of the day. Even though you have not made a stealth check, and they have not made a perception check, I would rule the situations above using surprise. The attack would be rolled top of the round.
I have found this ruling to be realistic, and give players the opportunity to exercise their agency.