YES, but it can only be done long term with an electric motor that is designed to also run as a generator and if the generator will be paralleled with other generation the motor will need to be a synchronous motor.
I served in the US Navy on a submarine as an electrician nuclear qualified. My electrical plant includes 2 - 500 KW motor generators. When the AC end was operating as an AC motor the DC end was a DC generator that supplied power to the sub's battery, reverse current and the DC end became a DC motor and the AC motor reversed current direction and operated as an AC generator. The speed and rotational direction of the common shaft AC-DC motor generator did not change regardless of which end was acting as the motor and which was acting as the generator.
I operated, repaired and maintained these motor generators personally for 3 years of my life, trust me I know what I am talking about. The only thing that reversed was current direction via raising or lowering resistance via rheostats.
Where you are not making the connection is you have to keep in mind the positive and negative terminal of the DC machine relative to the battery terminal voltages. If the positive post of the DC machine is for example 100 VDC, but battery terminal positive post voltage is 100.1 VDC, the current will flow out of the battery post, thru the DC motor and into the opposite post of the battery, and thru the acid to complete the circuit. If by raising the shunt current in the DC motor the DC machine terminals relative to the battery will be higher and the current will reverse direction and charge the battery.
Keep in mind that in every electric motor that is running there is generator action and every generator has motor action (counter EMF). AC motors don't require starting resistance, because of impedance of the windings and the AC frequency, this limits the in rush current on starting. However, DC motors do require starting resistors, because DC frequency is zero and therefore without a starting resistance the in rush is huge; however once the DC motor is spinning the starting resistors are out of the circuit, because the generator action (counter EMF) in the electric motor limits running current. It is by exploiting this counter EMF via shunt current that you can control the terminal voltage of the DC machine.
The other thing you must keep in mind is a DC machine has commutators, which allow the DC motor to operate otherwise it would turn 90 degrees and stop, but the commutator is constantly making and braking connections as the motor rotates, therefore the rotor field polarity relative to the stator poles stays correct and the electric motor continues to rotate. A series DC motor will run on AC, blenders, drill motors, etc are DC series motors (universal motors). Even though the terminals' polarity with AC will switch back and forth, because of the commutator and the fact that the same current flows in the field and stator the current doesn't reverse with AC supplied the DC motor rotates only a single direction.
With an AC generator in parallel with other AC generators if I try to raise frequency I will pick up KW, and if I try to raise voltage I will pick up KVAR. If I lower the frequency & voltage enough the generator will unload to an extent that the current reverse direction and the AC generator motors. With a DC series shunt motor by controlling the current passing thru the shunt cool the terminals of the DC machine will exceed or be below battery/DC system voltage. If the DC terminals are above battery voltage the current will flow into the battery, if the DC terminals fall below the battery voltage the current will reverse direction and current will flow out of the battery and the DC generator will become a DC motor without changing directions.