Three-dimensional MHD simulation of an emerging flux tube in the sun