Motion planning is an essential process for the navigation of unmanned aerial vehicles (UAVs) where they need to adapt to obstacles and different structures of their operating environment to reach the goal. This paper presents an optimal motion planner for UAVs operating in unknown complex environments. The motion planner receives point cloud data from a local range sensor and then converts it into a voxel grid representing the surrounding environment. A local trajectory guiding the UAV to the goal is then generated based on the voxel grid. This trajectory is further optimized using model predictive control (MPC) to enhance the safety, speed, and smoothness of UAV operation. The optimization is carried out via the definition of several cost functions and constraints, taking into account the UAV's dynamics and requirements. A number of simulations and comparisons with a state-of-the-art method have been conducted in a complex environment with many obstacles to evaluate the performance of our method. The results show that our method provides not only shorter and smoother trajectories but also faster and more stable speed profiles. It is also energy efficient making it suitable for various UAV applications.