Shame on your real estate agent for not taking better care of you and the lender too!
Leave the 401 alone.
Were you pre-approved for a mortgage before you wrote and signed a contract? If so you would have known what you needed up front via the Federally mandated Good Faith Estimate (GFE) that shows all estimated closing costs, down payment needed and the pre-paid cost. If you needed additional funds they could have built the closing costs into the sale/purchase agreementl. Since you now have the appraisal, it is after the fact. So here is a suggestions below.
Have the lender give a turn down letter because you have insuffucient funds to do the deal. They can issue the declination letter and then you may either negotiate the closing costs with the seller or maybe kill the deal, but certainly you'll be wiser. Go to the next deal with a new real estate BROKER not an affiliate or associate level licensee and a reputible lender who will advise you up front. You should also consult with an attorney to be sure of your legal rights and obligations in this deal.
All real estate sale agreements I know of have a mortgage contingency. If you can not remove the mortgage contingency by getting approved for the loan, then the deal likely dies without further liability.
Another possiblity is if you are a first timer, income is under set limits but otherwise can qualify for an FHA loan, there may be down payment assistance programs or closing cost assistance available to you that would allow you to move forward and close. I never like 100% financing but if you really like the home, the area, and the job is secure then there is nothing wrong with doing the deal, the price will catch up over time. If this is FHA loan and you own a car boat or some such you may borrow against that assett as long as it does not upset your ratio of debt to income. Again there may be a 100% loan available that would allow you to cover the closing and prepaid costs with the funds you now have. Your lender should also be looking at that option.
The lender may be able to assist with some closing costs as there is a spred on the loan lock date and the closing date that is worth a few hundred dollars that lenders typically do not tell you about because they pocket the "spred" themselves. It's your money, you need to demand it be applied to reduce your loan costs! I just got a reduction for my son form an on-line lender who did it to not loose the deal (he is in GA, I'm in TN, the lender is based in Michigan). Also demand the GFE and go over it in detail. You should not plead ignorance a 2nd time, it will fall on deaf ears.
Your real estate agent should have advised you on mortgages. I do what I call "mortgage 101" with everybody I assist in homebuying. We cover credit, loans, rates, adjustable vs fixed rate, etc. I have yet to do this with somebody who did not learn something they did not already know.
For your next real estate agent be sure they hold the GRI and CRS designations and if possible go for the grand slam, that being that they also hold the highest level of license the state offers (in TN that is simply "Broker"). That gets you a broker who has a ton of education, they can own and/or operate a real estate firm and have leaned more ways between now and Sunday to make a deal work legally and to your advantage.
I am happy to refer you to a high quality broker in your area if you want, just drop me a note stating so with how to contact you and what you want. I get p*ssed when I see deals like you described as I feel they are inexcusable in this day and age.
I am a licensed real estate broker in TN and the holder of GRI, CRB and CRS designations, with over 500 hrs of real estate specific education since 1994, and manager of my office.