Transform origin locally and object globally
To set the origin of an objects mesh to the global location of the scene cursor while maintaining the global location of the vertices, calculate the local coordinate of cursor, transform the mesh by subtracting it from all vert coordinates.
Then move the object back by the vector difference of starting global location (matrix world translation) and cursor location.
Test script: run in object mode.
import bpy
from mathutils import Matrix
context = bpy.context
scene = context.scene
ob = context.object
mw = ob.matrix_world
imw = mw.inverted()
me = ob.data
origin = scene.cursor.location
local_origin = imw @ origin
me.transform(Matrix.Translation(-local_origin))
mw.translation += (origin - mw.translation)
Note this will however move other objects that share the mesh.
For an edit mode version see
Setting mesh's origin in python (2.8)