If the Preferences window is the active one you can use bpy.ops.wm.window_close()
Otherwise you have to overwrite the context.
With Blender 3.2 there was a change.
Now we better use Context.temp_override(window, area, region, **keywords)
for this.
The following example should work with all versions:
edited: thanks to scurest
import bpy
bpy.ops.preferences.addon_show(module='io_scene_obj') # example
for window in bpy.context.window_manager.windows: # searching preference window
screen = window.screen
for area in screen.areas:
if area.type == 'PREFERENCES':
# Blender 3.2 and more
if bpy.app.version[0]version ==>= (3 and bpy.app.version[1], >2, 10):
from bpy import context
with context.temp_override(window=window, area=area):
bpy.ops.wm.window_close()
# Blender < 3.2 with override context
else:
override = {'window': window, 'screen': screen, 'area': area}
bpy.ops.wm.window_close(override)
break