Bristol City won a pulsating clash against their Severnside rivals Cardiff City in an eventful five-goal encounter.