Locked or crossed markets are generally not allowed except in extenuating circumstances where synchrony may be difficult, e.g. right after open or in the final minutes leading into the close.
What happens in a locked or crossed market depends on the venue and where a stock is listed.
For Nasdaq-listed stocks, the Nasdaq exchange as well as many ECNs and market makers will charge you a fee to route away. Often, that is in addition to any access fee charged at the venue where your order is sent. You can also access many venues via SuperMontage which can match orders and route orders to the best venue. However, that may (again) incur access fees. If you send orders that lock or cross other orders in the pre-market, your orders will go into the Nasdaq opening cross. Orders marked for the Nasdaq closing cross can only execute at Nasdaq and need not worry about locking or crossing the market for the closing cross.
NYSE- and AMEX-listed stocks may trade on SuperMontage and orders can route to the NYSE or AMEX. Again, fees may be charged to "route away" in addition to access fees charged by the venue. One difference is that the NYSE and AMEX have specialists. Therefore, one possibility if you submit an order is that the specialist may decide to stop the stock and trade with you if they can offer the same price as available elsewhere. However, some quotes on AMEX may be inaccessible via SuperMontage. In those cases, markets may stay locked or crossed for a few seconds. Again, orders in the pre-market are often sent in to the NYSE or AMEX opening auction; and, orders for the closing auctions can only be sent to the listing exchange (NYSE or AMEX).
The difference between the NYSE or AMEX and Nasdaq is, as it has historically been true, due to specialists versus competing market makers.
One important note if you do historical research with older price data: When there were short sales restrictions or when odd lots were not displayed in the NBBO, even weirder situations arose. For example, suppose a NYSE stock had just ticked down and traded at the bid. You could jump in front of other people by sending a sell short at the bid or lower. Because of the downtick, the order could not be filled at the bid or lower. This led to the stock getting stopped by the specialist and the short order getting "price improvement" to execute at the offer. Therefore, if you do historical research, I would be cognizant that executions might look strange for shorts or odd lots.
For more information on what happens with locked and crossed markets, you might enjoy reading Shkilko, Van Ness, and Van Ness (2008) about locked and crossed markets.