The earnings are apportioned, which is why "the withdrawal was nonqualified" is rarely the end of the analysis. Publication 970 sets out the arithmetic in two steps. Multiply the distributed earnings from box 2 of Form 1099-Q by a fraction whose numerator is the adjusted qualified education expenses paid during the year and whose denominator is the total distributed during the year; that product is the tax-free earnings. Subtract it from total distributed earnings and the remainder is what goes into income. Adjusted qualified education expenses are the year's qualified expenses reduced by tax-free educational assistance, which includes the tax-free part of scholarships and Pell Grants, veterans' educational assistance and employer-provided educational assistance. So a scholarship arriving late in the year can turn a distribution that was planned as fully qualified into a partly taxable one without anyone touching the account.
The five exceptions to the additional tax, and what each of them does not do. Section 530(d)(4)(B) waives the 10 percent, and only the 10 percent, where the distribution is made to a beneficiary or the beneficiary's estate on or after the designated beneficiary's death; is attributable to the beneficiary being disabled within the meaning of section 72(m)(7); is made on account of a scholarship, allowance or payment described in section 25A(g)(2), to the extent the distribution does not exceed that amount; is made on account of attendance at one of the five named United States service academies, to the extent the distribution does not exceed the costs of advanced education attributable to that attendance; or is includible in income solely because the same expenses were counted in figuring an education credit. In every case the income tax on the earnings still applies. The exception buys the penalty, not the tax.
One further provision travels with them and is worth naming so it is not mistaken for a general escape route. Subparagraph (C) disapplies the additional tax where a contribution is distributed together with the net income attributable to it before the first day of the sixth month of the following taxable year. It was written for the Coverdell, which has an annual contribution cap and therefore has excess contributions to return, and Publication 970 describes the mechanism only in its Coverdell chapter. It is not a route for pulling ordinary contributions back out of a 529 penalty-free, and it would not need to be: contributions are basis and come out untaxed anyway, so the only thing it could ever reach is the earnings on a very recent contribution.
Who is taxed is a separate question from what is taxed, and it is decided by where the money went. Publication 970 provides that the designated beneficiary "is considered the recipient only if the distribution is made (a) directly to the designated beneficiary, or (b) to an eligible educational institution for the benefit of the designated beneficiary", and that "otherwise, the account owner is considered the recipient". Form 1099-Q carries a checkbox at box 6 to be marked when the recipient is not the designated beneficiary. A parent who takes the money into their own bank account rather than paying the school directly has made themselves the taxpayer, which for a household where the parent's rate is higher than the student's is a decision worth making deliberately.
Two 60-day escape hatches that convert a nonqualified distribution into no distribution at all. Under section 529(c)(3)(C)(i) the includibility rule does not apply to a portion of a distribution transferred within 60 days to another qualified tuition program for the same beneficiary, to the account of another designated beneficiary who is a member of the family, or to an ABLE account, with a 12-month limit on same-beneficiary program-to-program transfers under (c)(3)(C)(iii). And under section 529(c)(3)(D), where a beneficiary receives a refund of qualified expenses from the institution, a recontribution to a 529 of which they are the beneficiary within 60 days of the refund, and not exceeding the refunded amount, is not treated as a distribution. A dropped course or a term cut short is the ordinary case, and the 60 days run from the refund rather than from the original withdrawal.
The trap that sits inside a beneficiary change. Section 529(c)(5)(A) says a distribution is generally not a taxable gift, but (c)(5)(B) applies the gift and generation-skipping transfer taxes to "a transfer by reason of a change in the designated beneficiary under the program (or a rollover to the account of a new beneficiary)" unless the new beneficiary is both a member of the family and "assigned to the same generation as (or a higher generation than) the old beneficiary". Redirecting an account from a child to a grandchild is therefore a transfer-tax event even though it is not an income-tax one, and it is not the sort of thing a plan's beneficiary-change form warns about.
Nothing reports this for you, and the plan will not withhold. Form 1099-Q reports the gross distribution, the earnings and the basis; it does not say whether the withdrawal was qualified, because the plan does not know what the money was spent on. Invest529's program description is blunt about the consequence: it "will not withhold taxes or penalties due on a Non-Qualified Withdrawal" and "the taxpayer is responsible for properly documenting and reporting taxes and penalties due". The taxable earnings go on Schedule 1 of Form 1040 and the additional tax is figured in Part II of Form 5329 and reported on Schedule 2. Receipts are the taxpayer's to keep.
State tax runs on its own track, and it can reach backwards. A state that gave a deduction or credit for the contribution may recapture it when a nonqualified withdrawal is made. Invest529 states that such withdrawals "may require the recapture of some or all amounts, if any, that the Account Owner deducted from their Virginia taxable income", and my529 that a Utah account owner who claimed the state credit and then makes a nonqualified withdrawal "will need to add back previously taken tax credits". Whether a state does this, and how far back, is a state-by-state question that has to be checked against the plan's own program description.