Mexican Sergio Perez, in his penultimate race for the Racing Point team and without a drive for next season, took the first win of his Formula One career in an extraordinary Sakhir Grand Prix.