This paper provides necessary and sufficient conditions of optimality for a variational problem involving a fractional derivative with respect to another function. Fractional Euler-Lagrange equations are proven for the fundamental problem and when in presence of an integral constraint. A Legendre condition, which is a second-order necessary condition, is also obtained. Other cases, such as the infinite horizon problem, with delays in the Lagrangian, and with high-order derivatives, are considered. A necessary condition that the optimal fractional order must satisfy is proved.Mathematics Subject Classification 2010: 26A33, 49K05, 34A08.