This article introduces a new class of basis functions, namely, the generalized Bernoulli polynomials (GBP). The GBP are adopted for solving nonlinear fractional optimal control problems (NFOCP) generated by nonlinear fractional dynamical systems (NFDS) and boundary conditions (BC). The corresponding operational matrices (OM) of fractional derivatives (FD) expand the solution of the problem in terms of the GBP. The method transforms the NFOCP into systems of nonlinear algebraic equations. First, the state and control variables are approximated by the GBP with unknown coefficients and parameters and substituted in the objective function, NFDS and BC. Then, the Gaussian quadrature rule and the OM of FD allow the formulation of a constrained problem, which is solved using Lagrange multipliers. The accuracy of the method is tested by means of several examples and the results confirm its good performance.