this buy order gets filled at 101.
The logic that works for order execution.. is Order Price or Best Price.
Best Price = price better then what is quoted in order.. Say, if someone is ready to buy at 102, then he will have no objection in buying at price less then 102.. Hence in above case 101, falls in this category then order is filled.
But if mkt price is 103, then it is not favourable price for buy order, hence it will just wait in the queue and remain pending..
For sell order, opposit logic works.. ie.. if there is sell order is at 105, then person will be happy to get any higher price then 105.. so if the buy queue has some buyer ready to pay higher then 105, the order gets filled.. But if next buyer is only going to pay 99,then order waits.. until, buyer increases its bid or some new buyer comes in who is ready to pay the asked price.
Hope this clarifies the order processing.
Happy Trading