Let's put it this way. When you send a message, it takes some time to reach the server, and then the server adds that message as the newset message in the chatlog. However, on your screen, you don't need to wait that time; it's immediately going to show up as the newest message.
Now let's assume someone else's message reaches the server before yours. That means that, chronologically, his message is older than yours. The server will then tell you someone else sent a message, but in your perspective, that message was sent AFTER you sent yours, meaning on your screen, that message will be the newset one, not your own message.
So it'll look something like this:
Let's assume time starts at t = 0
At t = 1, Person A sent a message
At t = 2, you sent your message, and the server receives Person A's message. Your screen shows that you have the newset message.
At t = 3, the server receives your message. At the same time, you receive Person A's message as an update from the server. Your screen shows that Person A now have the newset message, but the server received your message just now, putting your message as the newest message.
At t = 4, Person A receives your message as an update from the server.
Now let's assume you reload the page. The server will have to send the entire chatlog (or what's still visible) to you, meaning you will see what the server chronologically received. This means that, in your point of view, your message will now appear as the newest, thus seeming like they "swapped".
This is probably a gross oversimplification.
TLDR: The messages take time to get to the servers and update everyone else's devices, but for you, you sent the message instantly. When you reload, you sync up with the server, which may seem like messages have "swapped".