Not a lawyer and some states have weird issues but generally you can buy a long gun in any state so long as it goes through an FFL and the purchase of said long gun would be legal in both states. No face to face sales between private parties but you can go through an FFL.
And if you really want you can buy a handgun or stripped receiver that you can legally buy in MD if it is shipped to a MD (your home state) FFL who would then do the transfer for you. But the extra postage and transfer fees generally make it not worth the hassle but if the deal is great or you really want that particular gun it can be done.
Wal-Mart have their corporate policy and they basically will not sell any firearm to anyone from Maryland except in a store in Maryland.. The same is true for a whole list of other states that are anti-gun....
I've bought numerous long guns/shotguns in PA. One of the dealers said anything lever or bolt action only, no semi auto but he would transfer to a MD FFL. His store, his rules and I'm good with that. I've also had several handguns transferred with no problems.
Legally it's fine. But there's plenty of threads here talking about dealers who don't know the law. So yeah, it's fine, but you still might not get your gun.