Nonthermal loop-top sources in solar flares are the most prominent observational signatures that suggest energy release and particle acceleration in the solar corona. Although several scenarios for particle acceleration have been proposed, the origin of the loop-top sources remains unclear. Here we present a model that combines a large-scale magnetohydrodynamic simulation of a two-ribbon flare with a particle acceleration and transport model for investigating electron acceleration by a fast-mode termination shock (TS) at the loop top. Our model provides spatially resolved electron distribution that evolves in response to the dynamic flare geometry. We find a concave- downward magnetic structure located below the flare TS, induced by the fast reconnection downflows. It acts as a magnetic trap to confine the electrons at the loop top for an extended period of time. The electrons are energized significantly as they cross the shock front, and eventually build up a power-law energy spectrum extending to hundreds of kiloelectron volts. We suggest that this particle acceleration and transport scenario driven by a flare TS is a viable interpretation for the observed nonthermal loop-top sources.