A fusion algorithm combining Mahony and extended Kalman filter (EKF) is proposed to solve the problem of low accuracy of micro inertial measurement unit and large error of traditional attitude calculation method. First, the initial attitude quaternion is obtained by fusing gyroscope, accelerometer and magnetometer data with Mahony filter. Then, the attitude quaternion of the Mahony filter is used as the measurement value of EKF. According to the size of the non-gravity acceleration, the measurement noise covariance matrix is automatically adjusted by the positive correlation. The EKF equation of state is established according to the angular velocity information measured by the gyroscope. Finally, the attitude estimation of UAV is obtained after EKF filtering. The simulation results show that the static attitude angle error is less than 0. 1° and the dynamic attitude angle error is less than 1°, both of which are better than the complementary filtering algorithm and the improved EKF algorithm. The fusion algorithm can effectively suppress the gyro drift error, filter out the high frequency noise mixed with the measured value of accelerometer and suppress the interference of non-gravity acceleration, and improve the attitude calculation accuracy.