What happens if two people take buy/sell offer at same time?

What happens if two people see an offer to buy BTC, and fund the transaction costs to complete the trade at the same time? One person will get the trade. What about the other person? Are the btc returned to the person who tried to buy BTC was just a little bit later than the first person? Please let me know. Thanks.

What I mean is, if you send BTC to the transaction address, and someone beats you time-wise, how do you get your BTC back?

Your funds will not leave your wallet in such a case. You get a popup displayed that the offer is not available anymore when taking it.